- Home /
How do I clamp the camera to the boundaries of a game object I clicked on?
I have a simple 3D game object (a flattened cube that looks like a pizza box) that I can click on to select and identify using Raycast/LookAt. That's all working fine but once the camera is locked in the centre of this cube I want to be able to move the camera/mouse but only within the bounds of the cube face as it will contain buttons that I need to click on. It isn't a problem with LookAt because that is only called once when a boolean is set to false. Once it's called I set it to true so it only locks the camera once.
It's a little like in The Witness where you click on a panel and it becomes the focus and you can only move the reticle around the panel unless you escape out of it and you can then, once again, move around the game environment.
I'm using the FPS Character Controller along with a simple script to handle the player movement and camera rotation. My 'player' is just a cylinder with the main camera, a child object, attached to the top.
If anyone can help I'd be hugely grateful.
Answer by logicandchaos · Feb 23, 2021 at 02:04 PM
use Vector3.Distance() to check if the camera is in rage of the object. So something like if(Vector3.Distance(Camera.main.transform.position, selectedObject.transform.position)