如何统一停止相机跟随Y
Posted
技术标签:
【中文标题】如何统一停止相机跟随Y【英文标题】:How to stop camera following Y in unity 【发布时间】:2014-12-15 22:44:15 【问题描述】:我有这段代码,但我不知道如何让相机在我的玩家跳跃时停止跟随,在 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;
【问题讨论】:
顺便说一句,您可能希望将游戏开发 StackExchange 用于特定于游戏开发的事情 - 更多人准备回答有关该主题的问题。 【参考方案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;
【讨论】:
【参考方案2】:您可以为字符的 Y 值设置一个最小阈值。当角色跳跃时,您可以保存该变换点 y 并查找该点与角色位置之间的距离。y 如果角色超过该阈值,则您的相机可以跟随角色。此外,您可以考虑使用 Cinemachine。它非常强大。 (https://unity.com/unity/features/editor/art-and-design/cinemachine)
【讨论】:
以上是关于如何统一停止相机跟随Y的主要内容,如果未能解决你的问题,请参考以下文章