Home > Enterprise >  Camera will not follow player on positive x, y coordinates but will on negative x, y coordinates. Wh
Camera will not follow player on positive x, y coordinates but will on negative x, y coordinates. Wh

Time:09-20

Camera will not follow player on positive x, y coordinates but will on negative x, y coordinates. What did I do wrong? I'm following this tutorial https://www.youtube.com/watch?v=b8YUfee_pzc&t=3612s and it looks like I copied it to the letter but I can't get it to work. (Code starts at 55:39 in the video)

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

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

    // This is to check if we're inside the bounds on the X axis

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

    // This is to check if we're inside the bounds on the Y axis

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

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

}`

CodePudding user response:

The tutorial (and/or) you has some typos/mistakes there.

In the two cases

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

you are changing two different values. Once deltaX and once delta.x.

Later in the tutorial he made another mistake and uses

transform.position  = new Vector3(delta.x, deltaY);

so again once using the delta.x but the other time the deltaY which is unrelated to delta.y.

I suppose it should rather be

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

    // This is to check if we're inside the bounds on the X axis
    var 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;
        }
    }

    // This is to check if we're inside the bounds on the Y axis
    var 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  = delta;
}

probably could also simplify it a bit using e.g.

Vector2 delta = lookAt.position - transform.position;

if (Mathf.Abs(delta.x) > boundX)
{
    delta.x  = boundX * Mathf.Sign(delta.x);
}

if (Mathf.Abs(delta.y) > boundY)
{
    delta.y  = boundY * Mathf.Sign(delta.y);
}

transform.position  = (Vector3) delta;

but not sure on that last bit since typing on the phone here ;)

  • Related