首页 > 解决方案 > Unity 2018 相机跟随对象但用箭头键平移

问题描述

我对 Unity 很陌生(几天前开始)。

我正在尝试创建相机始终跟随滚动球的功能,但相机也应由箭头键控制,同时仍专注于主要对象。

我有以下代码跟随实际对象:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform player;
    private Vector3 offset = new Vector3(0, 3, -5);

    // Update is called once per frame
    void Update() {
        transform.position = player.position + offset;
    }
}

上面的代码完美运行,它在移动时跟随对象。

但我尝试了以下方法来移动相机:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform player;
    public GameObject MainCamera;
    public Vector3 offset = new Vector3(0, 3, -5);

    private Vector3 RotateOffset;
    public float rotateJump = 1f;

    // Update is called once per frame
    void Update() {

        //Rotate Camera 90 degrees Left
        if (Input.GetKey("left")) {
            RotateOffset = new Vector3(transform.position.x - rotateJump, 0, transform.position.z - rotateJump);
            transform.position = player.position + RotateOffset;
        }

        //Rotate Camera 90 degrees Right
        if (Input.GetKey("right")) {
            RotateOffset = new Vector3(transform.position.x + rotateJump, 0, transform.position.z + rotateJump);
            transform.position = player.position + RotateOffset;
        }
    }
}

但这只会将相机沿直线从位置 A 移动到 B,如下所示,不会旋转:

位置A: 在此处输入图像描述

位置 B: 在此处输入图像描述

我怎样才能让相机跟随物体并仍然由用户控制?

提前致谢!


我在板上移动对象的代码如下所示:

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public Rigidbody rb;
    public float MovementForce = 200f;

    void FixedUpdate() {
        //Move Forward
        if (Input.GetKey("w")) {
            rb.AddForce(0, 0, MovementForce * Time.deltaTime);
        }

        //Move Left
        if (Input.GetKey("a")) {
            rb.AddForce(-MovementForce * Time.deltaTime, 0, 0);
        }

        //Move Back
        if (Input.GetKey("s")) {
            rb.AddForce(0, 0, - MovementForce * Time.deltaTime);
        }

        //Move Right
        if (Input.GetKey("d")) {
            rb.AddForce(MovementForce * Time.deltaTime, 0, 0);
        }
    }
}

编辑:

我将 FollowPlayer 脚本更新为如下所示:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform player;
    private Vector3 offset = new Vector3(0, 3, -5);

    private float RotateVal = 2f;

    private Vector3 VPanAmount = new Vector3(2, 0, 0);
    private Vector3 HPanAmount = new Vector3(0, 2, 0);

    private Vector3 VRotatoinOffset;
    private Vector3 HRotatoinOffset;

    private void Start() {

    }

    // Update is called once per frame
    void Update() {

        if (Input.GetKey("left")) {
            VRotatoinOffset = player.position + offset - VPanAmount;
            transform.Rotate(0, -RotateVal, 0);
        }

        if (Input.GetKey("right")) {
            VRotatoinOffset = player.position + offset + VPanAmount;
            transform.Rotate(0, RotateVal, 0);
        }

        if (Input.GetKey("up")) {
            HRotatoinOffset = player.position + offset + HPanAmount;
            transform.Rotate(-RotateVal, 0, 0);
        }

        if (Input.GetKey("down")) {
            HRotatoinOffset = player.position + offset + HPanAmount;
            transform.Rotate(RotateVal, 0, 0);
        }

        transform.position = player.position + offset - VRotatoinOffset - HRotatoinOffset;
    }
}

相机随着物体的移动而旋转和移动,但相机的焦点不会停留在物体上,相机会跳到比赛场地的下方。

标签: unity3d

解决方案


推荐阅读