我怎样才能让相机从后面跟随玩家?
How can i make the camera to follow the player from behind?
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject player; //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 - player.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 = player.transform.position + offset;
transform.LookAt(player.transform);
}
}
如果我只使用这条线:
transform.LookAt(player.transform);
它会根据玩家的移动旋转摄像机,但摄像机会保持原位。
当我也使用这条线时:
transform.position = player.transform.position + offset;
镜头会移动,但现在它会在玩家的前面,而不是跟随它的后面。我想让相机从后面看。
你应该在播放器本地获取偏移量space。您可以使用矢量数学(例如,player.transform.position - player.transform.forward * distance
)或在玩家的层次结构中有一个引用空游戏对象作为相机的位置目标。
你的脚本是正确的;要让相机跟随玩家,您必须:
- 按照@apk 的建议,在层级视图中创建一个空的游戏对象并将其命名为"Player"、
- 将玩家和摄像机都添加到空游戏对象中。
创建空游戏对象时,不要忘记将其位置重置为 0, 0, 0。
希望对您有所帮助。
检查这个脚本,这将跟随带有相机永久注视矢量的目标,然后如果你想跟随目标的前向矢量需要向脚本添加额外的逻辑:)
public class CameraFollowController : MonoBehaviour
{
public GameObject target;
int offsetDistance = 10;
int offsetHeight = 5;
Vector3 targetLookAtVec;
void Start()
{
Camera.main.transform.position = target.transform.position
- target.transform.forward * offsetDistance
+ target.transform.up * offsetHeight;
Camera.main.transform.LookAt(target.transform);
targetLookAtVec = Camera.main.transform.position - target.transform.position;
}
void LateUpdate()
{
Camera.main.transform.position = target.transform.position + targetLookAtVec;
}
}
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject player; //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 - player.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 = player.transform.position + offset;
transform.LookAt(player.transform);
}
}
如果我只使用这条线:
transform.LookAt(player.transform);
它会根据玩家的移动旋转摄像机,但摄像机会保持原位。 当我也使用这条线时:
transform.position = player.transform.position + offset;
镜头会移动,但现在它会在玩家的前面,而不是跟随它的后面。我想让相机从后面看。
你应该在播放器本地获取偏移量space。您可以使用矢量数学(例如,player.transform.position - player.transform.forward * distance
)或在玩家的层次结构中有一个引用空游戏对象作为相机的位置目标。
你的脚本是正确的;要让相机跟随玩家,您必须:
- 按照@apk 的建议,在层级视图中创建一个空的游戏对象并将其命名为"Player"、
- 将玩家和摄像机都添加到空游戏对象中。
创建空游戏对象时,不要忘记将其位置重置为 0, 0, 0。
希望对您有所帮助。
检查这个脚本,这将跟随带有相机永久注视矢量的目标,然后如果你想跟随目标的前向矢量需要向脚本添加额外的逻辑:)
public class CameraFollowController : MonoBehaviour
{
public GameObject target;
int offsetDistance = 10;
int offsetHeight = 5;
Vector3 targetLookAtVec;
void Start()
{
Camera.main.transform.position = target.transform.position
- target.transform.forward * offsetDistance
+ target.transform.up * offsetHeight;
Camera.main.transform.LookAt(target.transform);
targetLookAtVec = Camera.main.transform.position - target.transform.position;
}
void LateUpdate()
{
Camera.main.transform.position = target.transform.position + targetLookAtVec;
}
}