简体   繁体   English

仅当播放器到达Unity C#中屏幕的顶部1/2时,才如何沿Y轴向上移动相机

[英]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.

相关问题 Unity:仅当角色到达 Y 轴上的某个点时才使用相机跟踪玩家的移动? - Unity: Track player movement with camera only when character reaches a certain point on the Y axis? 相机:如何阻止播放器在y轴上统一上下旋转? - Camera: How to stop the player turn up and down on y-axis in unity? 当我在 Unity 中的 Y 轴上跳跃时,如何让我的相机停止跟随我的玩家? - How can I get my camera to stop following my player when I jump on the Y axis in Unity? Unity脚本(C#)限制相机在y轴上的旋转 - Unity script (C#) to restrict camera rotation on the y-axis Unity 为什么在 3d 自上而下的游戏中,玩家无法转向 cursor? (仅 Y 轴)透视相机/LookAt - Unity Why can't the player turn towards the cursor in a 3d top-down game? (only Y-axis)Perspective Camera/LookAt C#Player在Unity中移动 - C# Player move in Unity 如何限制 Y 轴上的旋转,使玩家无法在 Unity 中旋转相机 360? - How can I limit the rotation on the Y axis so the player cant spin the camera 360 in Unity? 如何限制 Y 轴上的旋转,以便玩家无法在 Unity 中连续旋转相机 - How can I limit the rotation on the Y axis so that the player can't continuously spin the camera in Unity 如何以统一C#使敌人从屏幕顶部移动到屏幕底部? - How do i make enemy move from top to bottom of screen in unity c#? 如何在统一C#中围绕播放器旋转相机 - How to rotate camera around player in unity c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM