Search Unity

Camera Bound problem in positive Y axis (2D top down project)

Discussion in 'Getting Started' started by Sharker555, Dec 20, 2021.

  1. Sharker555

    Sharker555

    Joined:
    Dec 17, 2021
    Posts:
    2
    So i just started unity i am pretty new and trying to make my first game, to do that i am following a tutorial.
    but at one point on how to make the camera follow the player when he goes out of bounds i seem to run into a of an issue i wrote the code/ script and understood it but i can't seem to figure it out, the issue is that the bound is non existent in the positive y axis, as in the camera moves when the player move up there is no space or room, here is the code and i thanks in advance.

    public class Camera : MonoBehaviour
    {

    public Transform lookAt;
    public float boundX = 0.15f;
    public float boundY = 0.05f;


    private void LateUpdate()
    {
    Vector3 delta = Vector3.zero;

    float deltaX = lookAt.position.x - transform.position.x;
    if(deltaX > boundX || deltaX < -boundX)
    {
    if(transform.position.x < lookAt.position.x)
    {
    delta.x = deltaX - boundX;
    }
    else
    {
    delta.x = deltaX + boundX;
    }
    }


    float deltaY = lookAt.position.y - transform.position.y;
    if (deltaY > boundY || deltaY < -boundY)
    {
    if(transform.position.y < lookAt.position.y)
    {
    delta.y = deltaY - boundY;
    }
    else
    {
    delta.y = deltaY + boundY;
    }
    }

    transform.position += new Vector3(delta.x, delta.y, 0);

    }

    }
     
  2. Sharker555

    Sharker555

    Joined:
    Dec 17, 2021
    Posts:
    2
    Nevermind I changed Scenes and it works just fine
     
  3. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    If you're showing code, the regulars like it if you use code tags.