Placing a trigger on camera border

Hello, I’m trying to make a trigger right at where the camera border ends.

This is my code:

``````Trigger.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(-Screen.width   +
Camera.main.orthographicSize + x,
Screen.height - Camera.main.orthographicSize,
0));
``````

x is just some offset, because it would end way more to the right

This is what I want it to look like

The problem is: When the window is too small or too big (Wide or narrow monitors)
The object moves too far, it doesn’t follow the border

What else should I use to calculate the right position?
Also, what is the way of calculating the other side?
Thank you

The size of the camera front plane is determined by the proportions of the screen resolutions, therefore we must scale the orthographic size to find the position in world space.

``````   float scaleW = (float)Screen.width / Screen.height;
float posLeftX = Camera.main.transform.position.x - (Camera.main.orthographicSize * scaleW);
float posRightX = Camera.main.transform.position.x + (Camera.main.orthographicSize * scaleW);
triggerLeft.transform.position = new Vector3(posLeftX, Camera.main.transform.position.y, Camera.main.transform.position.z);
triggerRight.transform.position = new Vector3(posRightX, Camera.main.transform.position.y, Camera.main.transform.position.z);
``````

The scale in Y of the trigger should be like this:

``````trigger.localScale = new Vector3(1f, Camera.main.orthographicSize*2f, 1f);
``````
1 Like

Thank you, this works perfectly!