简体   繁体   English

当我在 Unity 中的 Y 轴上跳跃时,如何让我的相机停止跟随我的玩家?

[英]How can I get my camera to stop following my player when I jump on the Y axis in Unity?

Real quick.真快。 I'm making a 2D game where my player is moving downwards along the Y-axis.我正在制作一个 2D 游戏,我的玩家沿着 Y 轴向下移动。 Nothing brings the player back up so I would like to lock my camera on the player.没有什么能让播放器恢复原状,所以我想将我的相机锁定在播放器上。 When my player jumps, the camera follows which is basically what i'm trying to prevent but I cant seem to find an "easy" solution for this.当我的玩家跳跃时,相机跟随这基本上是我试图阻止的,但我似乎无法找到一个“简单”的解决方案。 Thanks in advance if anyone can help me out!'如果有人可以帮助我,请提前致谢!

  public Transform target;

  public bool X, Y, Z;

  public float XOffset, YOffset, ZOffset;

  void Update()
  {
        transform.position = new Vector3(
            (X ? target.position.x + XOffset : transform.position.x),
            (Y ? target.position.y + YOffset : transform.position.y),
            (Z ? target.position.z + ZOffset : transform.position.z));

  }

When Y is true, have the y component be the minimum of the current y position and the target's y position plus offset.Y为真时,y 分量是当前 y 位置和目标的 y 位置加上偏移量的最小值。

public Transform target;

public bool X, Y, Z;

public float XOffset, YOffset, ZOffset;

void Update() 
{ 
    Vector3 targetPos = target.position;
    Vector3 currentPos = transform.position;

    transform.position = new Vector3(
            X ? targetPos.x + XOffset : currentPos.x, 
            Y ? Mathf.Min(currentPos.y, targetPos.y + YOffset) : currentPos.y,
            Z ? targetPos.z + ZOffset : currentPos.z);
}

Another way would be to update a gounded_y variable every frame the player is on the ground and use that as the minimal or maximal (depending on direction) for the y of the camera.另一种方法是在玩家在地面上的每一帧更新一个 gounded_y 变量,并将其用作相机 y 的最小值或最大值(取决于方向)。 This way the cam will follow if the player falls down after a jump but will never follow upwards during a jump.这样,如果玩家在跳跃后跌倒,凸轮将跟随,但在跳跃过程中永远不会向上跟随。

You can store camera's last transform position and check if its higher than before.您可以存储相机的最后变换位置并检查它是否比以前高。 Then you can change Y value according to it.然后你可以根据它改变Y值。

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    Vector3 LastCameraPosition = Vector3.positiveInfinity;

    public Transform target;

    public bool X, Y, Z;

    public float XOffset, YOffset, ZOffset;

    void Update()
    {

        if (target.position.y < LastCameraPosition.y)
        {
            LastCameraPosition = target.position;
            Y = true;
        }
        else
        {
            Y = false;
        }

        transform.position = new Vector3(
       (X ? target.position.x + XOffset : transform.position.x),
       (Y ? target.position.y + YOffset : transform.position.y),
       (Z ? target.position.z + ZOffset : transform.position.z));
    }
}

if your player is always going downwards you can add a condition to the camera update如果您的播放器总是向下,您可以在相机更新中添加条件

if(character.transform.position.y < camera.transform.position.y){
    camera.transform.position = new Vector3(
            character.position.x + XOffset,
            character.position.y + YOffset,
            character.position.z + ZOffset);
}

of course in this case the camera isn't allowed to be a child object of the player当然在这种情况下,相机不允许成为玩家的子对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM