0
這是原來的代碼使相機跟隨玩家:如何讓用戶可以選擇相機是否在播放器後面?
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - objectToFollow.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = objectToFollow.transform.position + offset;
transform.LookAt(objectToFollow.transform);
}
}
而這正是我試圖做的,但隨後球員它的自我(ThirdPersonController)不旋轉根據他移動。
隨着他原來的腳本。
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
public bool behindPlayer = false;
private Vector3 cameraStartPos;
// Use this for initialization
void Start()
{
cameraStartPos = transform.position;
// Put the camera behind the player
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
}
private void Update()
{
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
else
{
transform.position = cameraStartPos;
}
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
transform.position = objectToFollow.transform.position;
transform.LookAt(objectToFollow.transform);
}
}
我想,相機會自動將玩家已經落後,而無需更改現場攝像機的位置,如果用戶希望使用布爾變量。
但腳本現在沒有在上面,如:
這條線在LateUpdate:
transform.position = objectToFollow.transform.position;
使用布爾假/真它不改變相機位置的。 如果沒有這條線,玩家將會移動並且相機會跟隨,但是玩家不會像以上那樣旋轉和移動。
我想要的就像第一個腳本,但與behindPlayer變量。