大家好,這里的初學者,我正在處理移動車輛挑戰,我可以讓相機跟隨卡車,也可以讓相機在視圖(駕駛員視圖/后視圖)之間切換問題是當我切換到后視圖時初始 x相機的旋轉設定為 0,所以我希望相機在驅動程式視圖中跟隨玩家 x 方向,這樣我就不會丟失后視圖方向,你可以在下面看到我的代碼和專案包的鏈接,謝謝你
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);
}
}
uj5u.com熱心網友回復:
我認為擁有多個相機并在它們之間切換會更容易。因此,創建您的 2 個攝像頭,為它們添加父約束 并根據需要進行設定。然后創建一個腳本來啟用和禁用攝像頭,如下所示:
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);
}
}
}
出于某種原因,在攝像機之間切換的最簡單方法是啟用和禁用它們。所以這個腳本就是這樣做的。請記住將您的相機拖到檢查器中腳本的插槽 cam1 和 cam2 中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461615.html
