我如何告诉相机只跟随 gameObject x 方向
How do i tell the camera to only follow the gameObject x orientation
各位初学者大家好,我正在研究移动车辆挑战,我可以让相机跟随卡车,也可以让相机在视图之间切换(driver view/back 视图)问题是当我切换到后视图时,相机的初始 x 旋转设置为 0,所以我希望相机在 driver 视图中跟随玩家 x 方向,这样我就不会丢失后视图方向,你可以在下面看到我的代码和项目包here的link,谢谢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
//Player GameObject variable (the vehicle)
public GameObject player;
//Fixing the camera vertical position
private Vector3 offset = new Vector3(0, 5, -7);
private Vector3 offset2 = new Vector3(0, 2.5f, 0.3f);
private int currentTarget;
public bool camController;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
camController = Input.GetButtonDown("Fire1");
if (camController) {
if (offset == offset2)
{
currentTarget = 2;
} else
{
currentTarget = 1;
}
switch(currentTarget)
{
case 1:
offset = offset2;
break;
case 2:
offset = new Vector3(0, 5, -7);
break;
}
}
//Offset the camera behind the player by adding to the player's position
transform.position = player.transform.position + offset;
transform.rotation = player.transform.rotation;
Debug.Log(camController);
}
}
我认为拥有多个摄像头并在它们之间切换会更容易。所以创建你的 2 个相机,添加 Parent Constraints
给他们并根据需要进行设置。然后创建一个启用和禁用摄像头的脚本,如下所示:
using UnityEngine;
public class SwitchCams : MonoBehaviour
{
public GameObject cam1;
public GameObject cam2;
bool isCam1 = true;
void Start(){
cam1.SetActive(true);
cam1.SetActive(false);
}
void Update(){
bool shouldSwitch = Input.GetButtonDown("Fire1");
if(shouldSwitch){
isCam1 = !isCam1;
cam1.SetActive(isCam1);
cam2.SetActive(!isCam1);
}
}
}
出于某种原因,在 unity 中切换摄像机的最简单方法是启用和禁用它们。所以这个脚本就是这样做的。
请记住将您的相机拖到检查器中脚本的插槽 cam1 和 cam2 中。
各位初学者大家好,我正在研究移动车辆挑战,我可以让相机跟随卡车,也可以让相机在视图之间切换(driver view/back 视图)问题是当我切换到后视图时,相机的初始 x 旋转设置为 0,所以我希望相机在 driver 视图中跟随玩家 x 方向,这样我就不会丢失后视图方向,你可以在下面看到我的代码和项目包here的link,谢谢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
//Player GameObject variable (the vehicle)
public GameObject player;
//Fixing the camera vertical position
private Vector3 offset = new Vector3(0, 5, -7);
private Vector3 offset2 = new Vector3(0, 2.5f, 0.3f);
private int currentTarget;
public bool camController;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
camController = Input.GetButtonDown("Fire1");
if (camController) {
if (offset == offset2)
{
currentTarget = 2;
} else
{
currentTarget = 1;
}
switch(currentTarget)
{
case 1:
offset = offset2;
break;
case 2:
offset = new Vector3(0, 5, -7);
break;
}
}
//Offset the camera behind the player by adding to the player's position
transform.position = player.transform.position + offset;
transform.rotation = player.transform.rotation;
Debug.Log(camController);
}
}
我认为拥有多个摄像头并在它们之间切换会更容易。所以创建你的 2 个相机,添加 Parent Constraints 给他们并根据需要进行设置。然后创建一个启用和禁用摄像头的脚本,如下所示:
using UnityEngine;
public class SwitchCams : MonoBehaviour
{
public GameObject cam1;
public GameObject cam2;
bool isCam1 = true;
void Start(){
cam1.SetActive(true);
cam1.SetActive(false);
}
void Update(){
bool shouldSwitch = Input.GetButtonDown("Fire1");
if(shouldSwitch){
isCam1 = !isCam1;
cam1.SetActive(isCam1);
cam2.SetActive(!isCam1);
}
}
}
出于某种原因,在 unity 中切换摄像机的最简单方法是启用和禁用它们。所以这个脚本就是这样做的。 请记住将您的相机拖到检查器中脚本的插槽 cam1 和 cam2 中。