Limits

Greetings,

I’m trying to set physical limits based on screen view.
The camera has a fixed position, and the character must stay within the red perimeter so it doesn’t go off-screen.

I was trying a couple of things:

  • Putting empty game objects with colliders. → If the character is pushed by another object, it goes through and is stuck inside/on the other side.

  • Parenting the character to the camera, and keeping the transform.position within localPositions. → The best solution so far, but would not work on every resolution, and it gives a squared area so the character can be off-screen when close and on the side of the cam.

  • Raycasting from each corner of the NearPlane towards each corner of the FarPlane. → The ray doesn’t follow the ground and actually goes through it a few units from the cam. And I wouldn’t be sure how I would keep the character inside the perimeter anyway.

Can you please help me? :slight_smile:

Clamping the character’s x position would be a good solution.

Set the min and max x positions and let Mathf.Clamp do the rest :slight_smile:

Vector3 pos = transform.position;

pos.x = Mathf.Clamp(pos.x, minXLimit, maxXLimit);

transform.position = pos;

You can use Camera.WorldToScreen

Vector2 screenPos = Camera.main.WorldToScreenPoint(player.transform.position);

Then you can work with your player position in screen space to constrain it.

(Allow it to move between 0 and Screen.width for example).

Or the other way around if it fit more your needs:

Vector3 leftBottom = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, player.transform.position.z));
Vector3 rightBottom = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, player.transform.position.z));

Then you can do what Intense_Gamer94 said:

pos.x = Mathf.Clamp(pos.x, leftBottom.x, rightBottom.x);

Thank you folks for the replies! It was very helpful and sent me on the right tracks!

I ended up with this, and it’s working fine!

        Vector3 leftBottom = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 3.6f));
        Vector3 leftTop = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 7.2f));
        Vector3 rightBottom = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 3.6f));
        Vector3 rightTop = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 7.2f));

        Vector3 leftLimit = new Vector3(leftBottom.x - (character.position.z - leftBottom.z), 0, character.position.z);
        leftTop = new Vector3(leftTop.x, 0, leftTop.z);
        Vector3 rightLimit = new Vector3(rightBottom.x + (character.position.z - rightBottom.z), 0, character.position.z);
        rightTop = new Vector3(rightTop.x, 0, rightTop.z);

        if (character.position.x < leftLimit.x) character.position = new Vector3(leftLimit.x, 0, character.position.z);
        if (character.position.x > rightLimit.x) character.position = new Vector3(rightLimit.x, 0, character.position.z);

        if (character.position.z < leftBottom.z) character.position = new Vector3(character.position.x, 0, leftBottom.z);
        if (character.position.z > leftTop.z) character.position = new Vector3(character.position.x, 0, leftTop.z);
1 Like

A piece of advice:

It’s better to modify an existing class than creating one.
In your script you are creating 8 Vector3 per frame (I guess), it also mean the garbage collector have to destroy 8 Vector3 per frame.

Any piece of advice are always more than welcome.
This is how I made it:
Can it be improved further?

Camera cam;
    public Transform character = null;
    Vector3 leftBottom;
    Vector3 leftTop;
    Vector3 rightBottom;
    Vector3 rightTop;
    Vector3 leftLimit;
    Vector3 rightLimit;

    void Awake()
    {
        cam = GetComponent<Camera>();
    }

    void Start()
    {
        leftBottom = cam.ScreenToWorldPoint(new Vector3(0, 0, 3.6f));
        leftTop = cam.ScreenToWorldPoint(new Vector3(0, 0, 7.2f));
        rightBottom = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, 3.6f));
        rightTop = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, 7.2f));
        leftTop = new Vector3(leftTop.x, 0, leftTop.z);
        rightTop = new Vector3(rightTop.x, 0, rightTop.z);
    }

    void Update()
    {
        if (character == null) return;

        leftLimit = new Vector3(leftBottom.x - (character.position.z - leftBottom.z), 0, character.position.z);       
        rightLimit = new Vector3(rightBottom.x + (character.position.z - rightBottom.z), 0, character.position.z);

        if (character.position.x < leftLimit.x) character.position = new Vector3(leftLimit.x, 0, character.position.z);
        if (character.position.x > rightLimit.x) character.position = new Vector3(rightLimit.x, 0, character.position.z);

        if (character.position.z < leftBottom.z) character.position = new Vector3(character.position.x, 0, leftBottom.z);
        if (character.position.z > leftTop.z) character.position = new Vector3(character.position.x, 0, leftTop.z);
    }

Not tested but try this:

Note that you never use Y and Z of leftLimit and rightLimit so you could change it to a float instead of a Vector3

    void Update()
    {
        if (character == null) return;

        leftLimit.x = leftBottom.x - (character.position.z - leftBottom.z);
        rightLimit.x = rightBottom.x + (character.position.z - rightBottom.z);

        Vector3 characterPos = character.transform.position;
        characterPos.x = Mathf.Clamp(characterPos.x, leftLimit.x, rightLimit.x);
        characterPos.z = Mathf.Clamp(characterPos.z, leftBottom.z, leftTop.z);
        character.transform.position = characterPos;
    }

Working fine as well!
Thanks!

Your code breaks if the game’s resolution is changed. If the player is playing in windowed mode and changes the window size, that happens.

If you want to handle that, you can check each frame if Screen.width or Screen.height has changed, and re-calculate the limits if that happened.

Vector3 is a struct, so this is wrong. The optimization still makes sense, but the problem with the code is that the ScreenToWorldPoint is calculated over again a bunch of times, not that any garbage is generated.

1 Like

Something like this?

    Camera cam;
    public Transform character = null;
    int screenWidth;
    Vector3 leftBottom;
    Vector3 leftTop;
    Vector3 rightBottom;
    Vector3 rightTop;
    Vector3 leftLimit;
    Vector3 rightLimit;

    void Awake()
    {
        cam = GetComponent<Camera>();
    }

    void Start()
    {
        leftBottom = cam.ScreenToWorldPoint(new Vector3(0, 0, 3.6f));
        leftTop = cam.ScreenToWorldPoint(new Vector3(0, 0, 7.2f));
        rightBottom = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, 3.6f));
        rightTop = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, 7.2f));
        leftTop = new Vector3(leftTop.x, 0, leftTop.z);
        rightTop = new Vector3(rightTop.x, 0, rightTop.z);
        screenWidth = Screen.width;
    }

    void Update()
    {
        if (character == null) return;

        if (Screen.width != screenWidth) SetWindowChanges();

        leftLimit.x = leftBottom.x - (character.position.z - leftBottom.z);
        rightLimit.x = rightBottom.x + (character.position.z - rightBottom.z);

        Vector3 characterPos = character.position;
        characterPos.x = Mathf.Clamp(characterPos.x, leftLimit.x, rightLimit.x);
        characterPos.z = Mathf.Clamp(characterPos.z, leftBottom.z, leftTop.z);
        character.position = characterPos;
    }

    void SetWindowChanges()
    {
        leftBottom = cam.ScreenToWorldPoint(new Vector3(0, 0, 3.6f));
        leftTop = cam.ScreenToWorldPoint(new Vector3(0, 0, 7.2f));
        rightBottom = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, 3.6f));
        rightTop = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, 7.2f));
        leftTop = new Vector3(leftTop.x, 0, leftTop.z);
        rightTop = new Vector3(rightTop.x, 0, rightTop.z);
        screenWidth = Screen.width;
    }

Yup. Start() should be this, though:

void Start() 
{
    SetWindowChanges();
}

:slight_smile:

(Also I think something like “FindBorders” is better than “SetWindowChanges”? What are window changes, and what are you setting them to?)

1 Like

I always find weird names for my methods. :stuck_out_tongue:

Thanks for the help! :slight_smile: