如何在Y中统一停止相机

时间:2014-10-20 20:27:05

标签: c# unity3d

我有这个代码,我无法弄清楚当他跳跃时,如何使相机停止跟随我的播放器,在unity3d

使用UnityEngine; 使用System.Collections;

公共类Camera2DFollow2: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;       
}

}

2 个答案:

答案 0 :(得分:1)

尝试

Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
newPos.y = transform.position.y;
transform.position = newPos;

Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
aheadTargetPos.y = transform.position.y;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;

答案 1 :(得分:0)

您可以为字符的 Y 值设置一个最小阈值。当角色跳跃时,您可以保存该变换点 y 并查找该点与角色位置之间的距离。如果角色超过该阈值,则您的相机可以跟随该角色。此外,您可以考虑使用 Cinemachine。它非常强大。 (https://unity.com/unity/features/editor/art-and-design/cinemachine)