简体   繁体   English

如何让相机从后面跟随玩家?

[英]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);
    }
}

If i will use only the line:如果我将只使用该行:

transform.LookAt(player.transform);

It will rotate the camera according to the player movement but the camera will stay in place.它会根据玩家的运动旋转相机,但相机会保持原位。 When i'm using also the line:当我也使用这条线时:

transform.position = player.transform.position + offset;

The camera will move but now it will be in front of the player not behind following it.相机会移动,但现在它会在玩家前面而不是后面。 I want the camera to LookAt and to follow from behind.我希望相机可以观察并从后面跟随。

You should obtain the offset in the player's local space.您应该获得玩家本地空间中的偏移量。 You can either use vector math (for example, player.transform.position - player.transform.forward * distance ) or have a reference empty GameObject in the player's hierarchy that serves as the camera's position target.您可以使用矢量数学(例如, player.transform.position - player.transform.forward * distance )或在玩家的层次结构中使用一个参考空游戏对象作为相机的位置目标。

check this script, this will follow the target with permanent lookat vector of the camera, then if you would like to follow forward vector of the target need to add additional logic to the script :)检查这个脚本,这将跟随相机的永久观察向量的目标,然后如果你想跟随目标的前向向量需要向脚本添加额外的逻辑:)

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;
        }
    }

Your script is correct;你的脚本是正确的; to make the camera follow the player you must:要使相机跟随玩家,您必须:

  1. as @apk suggested, create an empty game object in the hierarchy view and name it "Player",正如@apk 建议的那样,在层次结构视图中创建一个空的游戏对象并将其命名为“玩家”,
  2. add both the player and the camera to the empty game object.将玩家和相机添加到空游戏对象中。

When you create the empty game object, do not forget to reset its position to 0, 0, 0.创建空游戏对象时,不要忘记将其位置重置为 0, 0, 0。

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM