![](/img/trans.png)
[英]Unity: Track player movement with camera only when character reaches a certain point on the Y axis?
[英]How to move the camera up the Y axis only when the player reaches the top 1/2 of the screen in Unity C#
这是用于2D平台游戏。
播放器跳跃时,我不希望相机沿Y轴向上移动。 我只希望它在播放器移至屏幕上方时移动,以便它可以向上滚动到垂直平台和梯子。
有人知道要在代码和Unity编辑器中输入什么内容吗?
这是我到目前为止在相机脚本中提供的代码。
public class CameraControl : MonoBehaviour {
public GameObject target;
public float followAhead;
public float smoothing;
private Vector3 targetPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
targetPosition = new Vector3 (target.transform.position.x, transform.position.y, transform.position.z);
if (target.transform.localScale.x > 0f) {
targetPosition = new Vector3 (targetPosition.x + followAhead, targetPosition.y, targetPosition.z);
} else {
targetPosition = new Vector3 (targetPosition.x - followAhead, targetPosition.y, targetPosition.z);
}
transform.position = Vector3.Lerp (transform.position, targetPosition, smoothing * Time.deltaTime);
}
}
我猜您有一个与跳跃相关的布尔值,会触发跳跃动画。
因此,在相机的Update()
中,您可以执行以下操作:
void Update() {
// Update camera X position
if (isPlayerJumping) return;
// Update camera Y position
}
这样,仅当播放器没有跳跃时才更新相机的Y位置,而在所有情况下(即使在跳跃时)仍会更新X位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.