- Home /
How can I keep a player inside the camera box? [C#]
So I would basically like to keep a moving object to stay inside the camera box but I don't want the camera to follow this object, Id like it so that the camera is moving along the Y axis and the object can be moved freely within the camera view? How could i achieve this?
Answer by Konomira · Oct 11, 2015 at 03:30 PM
camera.ViewportToWorldPoint allows you to transform screen co-ordinates to world co-ordinates. Try using that to keep the character within the bounds of the screen http://docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html
Still having some trouble getting the camera to actually move, sadly. :(
Answer by fguinier · Oct 14, 2015 at 01:47 PM
Alternatively you can get the frustum planes from the camera : http://docs.unity3d.com/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html
Have a good day
Florent
Answer by Alsador · Oct 15, 2015 at 02:22 PM
I appreciate the help, but i have figured it out! Wanted to keep it simple, so i came up with this.
public class CameraTracksPLayer : MonoBehaviour {
// Update is called once per frame
void Update ()
{
float camUp = Time.deltaTime * 5;
transform.Translate (0, camUp, 0, Camera.main.transform);
}
}
I just added box colliders around the camera to restrict the player from moving out of it.
Your answer
Follow this Question
Related Questions
Camera and Player position 0 Answers
Don'tDetroyOnLoad | issue 1 Answer
Help|MouseLook Y-Axis moving in the wrong direction 1 Answer
Multiple Targets Camera 1 Answer