统一:使摄像机仅跟随玩家的 x 方向

本文关键字:玩家 方向 跟随 摄像机 统一 | 更新日期: 2023-09-27 18:32:43

我使用以下脚本,它使摄像机跟随播放器(在x和y方向上):

using UnityEngine;
using System.Collections;
public class Camera2DFollow : MonoBehaviour {
    public Transform target;
    public float damping = 1;
    public float lookAheadFactor = 3;
    public float lookAheadReturnSpeed = 0.5f;
    public float lookAheadMoveThreshold = 0.1f;
    float offsetZ;
    Vector3 lastTargetPosition;
    Vector3 currentVelocity;
    Vector3 lookAheadPos;
    // Use this for initialization
    void Start () {
        lastTargetPosition = target.position;
        offsetZ = (transform.position - target.position).z;
        transform.parent = null;
    }
    // Update is called once per frame
    void Update () {
        // only update lookahead pos if accelerating or changed direction
        float xMoveDelta = (target.position - lastTargetPosition).x;
        bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
        if (updateLookAheadTarget) {
            lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
        } else {
            lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);  
        }
        Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
        Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
        transform.position = newPos;
        lastTargetPosition = target.position;       
    }
}

但是,我只希望它在x方向上跟随玩家。玩了一段时间的剧本,但想不通。

统一:使摄像机仅跟随玩家的 x 方向

这似乎是示例资产中提供的脚本,它可能比您需要的更复杂。

我已经通过简单地使用以下方法实现了您之前尝试执行的操作:

void Update () {
    transform.Translate(Vector3.right * Time.deltaTime * movementSpeed);
}

这是来自附加到主相机的脚本。