Forum rules - please read before posting.

Camera resets position when Player enters scene

Hi,

I've setup a Camera with Coordinates (0, 5, -10) and enabled lock-Y, follow Player character.

When I start the scene from the Editor it works as expected. Camera only follows Player on the X axis, Y stays put.
But when I enter the scene from another room the Y position got reset to 0.

I guess it is related to the Player not spawned in the scene when the Camera inits itself.
Any ideas for a hotfix?

Best.
Alex

Comments

  • In GameCamera.cs line 132
    if (!haveSetOriginalPosition && backgroundConstraint && Camera.orthographic && (limitHorizontal || limitVertical) && Target) {
    Transform.position = new Vector3 (0f, 0f, Transform.position.z);
    }

    I think it is a bug because it resets both x and y coordinate to zero.
    It should make a distinction if limitHorizontal or limitVertical is false and only reset the respective axis.

  • edited October 2022

    I tried to fix it by only resetting the axis which is not locked. This works during gameplay. But when I save and load from another scene, the axis gets resetted again / camera works not properly.

    Please help.

  • I think it is a bug because it resets both x and y coordinate to zero.

    Could well be, though I'll need to recreate the original issue to confirm.

    It sounds like you're using a Background Constraint to restrict the camera's movement - is this the case?

    Please share your AC/Unity versions, as well as the full Inspector of the GameCamera.

  • Yes, Background Constraint is set. Camera is inside the constraint.

    AC Version: v1.75.4
    Unity: 2020.3.33f1

    Screenshot of Camera: https://www.dropbox.com/s/udh8t2qzeq4pazu/Camera-Inspector-Screenshot 2022-10-18 110557.png

  • Thank you, I will look into this.

  • This may be similar to what you've corrected already, but here's a replacement for the GameCamera2D's ForceRecordOriginalPosition function:

    public void ForceRecordOriginalPosition ()
    {
        if (!haveSetOriginalPosition && backgroundConstraint && Camera.orthographic && (limitHorizontal || limitVertical) && Target)
        {
            if (limitHorizontal && limitVertical)
            {
                Transform.position = new Vector3 (0f, 0f, Transform.position.z);
            }
            else if (limitHorizontal)
            {
                Transform.position = new Vector3 (0f, Transform.position.y, Transform.position.z);
            }
            else if (limitVertical)
            {
                Transform.position = new Vector3 (Transform.position.x, 0f, Transform.position.z);
            }
        }
    
        originalPosition = Transform.position;
        haveSetOriginalPosition = true;
    }
    

    I can't reproduce the issue of the camera resetting upon loading - but be sure to attach a Constant ID component to your camera. You should be seeing a Console warning about this not being present after loading.

  • edited October 2022

    Thanks Chris!

    Constant ID did the trick for saving and loading!

    Yes, your code is similar to mine. But I think you mixed up the resetting values in your script. When limitHorizontal is set then it should keep its x-position and not resets it to 0. Likewise with limitVertical.

    Best.
    Alex

  • edited October 2022

    When limitHorizontal is set, then the position is determined by the background constraint. The position in that direction needs to be zero'd, while the un-constrained axis should be unaffected.

  • edited October 2022

    Ah of course. I mixed it up sorry! :#

    What I actually meant was not limitHorizontal/Vertical but lockHorizontal/Vertical.

    I fixed it like this:

    if (!haveSetOriginalPosition && backgroundConstraint && Camera.orthographic && (limitHorizontal || limitVertical) && Target)
    {
        Vector3 pos = Transform.position;
    
        Transform.position = new Vector3 (lockHorizontal? pos.x : 0, lockVertical? pos.y : 0f, Transform.position.z);
    }
    
  • Understood, thanks.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.