[英]How to move the camera up the Y axis only when the player reaches the top 1/2 of the screen in Unity C#
This is for a 2D platform game. 这是用于2D平台游戏。
I don't want the camera to move up the Y axis when the player jumps. 播放器跳跃时,我不希望相机沿Y轴向上移动。 I only want it to move when the player to the upper part of the screen so it can scroll up to vertical platforms and ladders. 我只希望它在播放器移至屏幕上方时移动,以便它可以向上滚动到垂直平台和梯子。
Does anybody know what to enter in the code and the Unity editor so that can be done? 有人知道要在代码和Unity编辑器中输入什么内容吗?
Here's the code I have so far in the camera script. 这是我到目前为止在相机脚本中提供的代码。
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);
}
}
I guess you have a bool tied to the jump which triggers the jumping animation. 我猜您有一个与跳跃相关的布尔值,会触发跳跃动画。
So, in the Update()
of the camera you can do something like this: 因此,在相机的Update()
中,您可以执行以下操作:
void Update() {
// Update camera X position
if (isPlayerJumping) return;
// Update camera Y position
}
This way, you update the Y position of the camera only if the player isn't jumping, while still updating the X position in all cases (even while jumping). 这样,仅当播放器没有跳跃时才更新相机的Y位置,而在所有情况下(即使在跳跃时)仍会更新X位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.