Padding switches back to default

Hey guys!

I have daringly decided to change my way of padding. It kinda works (like this):

public class Player : MonoBehaviour

{
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float xMin;
    [SerializeField] float xMax;
    [SerializeField] float yMin;
    [SerializeField] float yMax;

    // Start is called before the first frame update
    void Start()
    {
        SetUpMoveBoundaries();
    }

    private void SetUpMoveBoundaries()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y;
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }
    private void Move()
    {
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

        var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
        var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);

        transform.position = new Vector2(newXPos, newYPos);
    }
}

As you can see I serialized the boundary for each side to add the numbers in the editor. Which works well for me and I have found good values to work with:
Bildschirmfoto 2021-12-15 um 16.24.50

But everytime I enter play mode it switches back to these values:
Bildschirmfoto 2021-12-15 um 16.25.09

Which are the values the pivot creates when hitting the boundary. I can not figure out though why it is not using the values I put in manually. Anybody got an idea why?

Thanks for your help!

Does this script exist on a prefab? Maybe you’re editing the prefab and not the instance in the scene? Or if you edit the values while play testing they will revert to what they were.

Hope this helps

Hey Riley!

Nah, it’s not a prefab and I changed the values outside the playmode. It’s the other way around. I change them and they revert back to shown values in playmode (blue tint).

I think I got it… I am setting the xMax (and so forth) value twice. Once in my serialized float and then again in the private void SetUpMoveBoundaries(). I think that’s causing the issue.

Does that mean you fixed the problem, @TheSnooze? :slight_smile:


See also:

I did indeed. Should’ve done what the course suggested. :smiley:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms