简体   繁体   中英

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. 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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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