如何使用户可以选择相机是否在播放器后面?

时间:2017-06-30 17:59:25

标签: c# unity3d unity5

这是原始代码让相机跟随播放器:

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

如果用户想要使用bool变量,我希望相机在播放器后面是自动的,无需更改场景中的相机位置。

但是剧本现在不像上面那样:

在LateUpdate中使用此行:

transform.position = objectToFollow.transform.position;

使用bool false / true它根本不改变相机位置。 没有这条线,玩家将移动并且相机将跟随,但玩家将不会旋转而不会如上所述移动。

我想要的就像第一个脚本,但是使用了LaterPlayer变量。

1 个答案:

答案 0 :(得分:0)

工作脚本:

IDbConnection

在检查器中,将偏移量设置为例如0,5,-12和目标集,例如ThirdPersonController。并将脚本附加到相机(如果目标是ThirdPersonController,则必须将相机标记为MainCamera,因为ThirdPersonController ThirdPersonUserControl脚本正在寻找MainCamera)。

相机不必是目标的孩子,相机可以在层次结构中的任何位置。

无论如何它正在工作。