- Home /
How to set a limit for maximum camera scrolling?
I managed to make my camera scroll when the "up", "down", "left" and "right" key is pressed, now I would like to set a limit for the maximum unit that the camera can scroll in the scene, how do I go about doing it, can anyone kindly help me?
void OnGUI ()
{
if ( Input.GetKey("up") )
{
Camera.main.transform.Translate( Vector3.up * Time.deltaTime * scrollSpeed, Space.World );
}
if ( Input.GetKey("down") )
{
Camera.main.transform.Translate( Vector3.down * Time.deltaTime * scrollSpeed, Space.World );
}
if ( Input.GetKey("right") )
{
Camera.main.transform.Translate( Vector3.right * Time.deltaTime * scrollSpeed, Space.World );
}
if ( Input.GetKey("left") )
{
Camera.main.transform.Translate( Vector3.left * Time.deltaTime * scrollSpeed, Space.World );
}
}
Comment
Answer by robertbu · Apr 03, 2013 at 08:51 AM
Add a condition to each one of your 'if' statements. Decide what is the maximum and minimum values for both up/down and left/right. The 'if' statements would look something like:
if ( Input.GetKey("up") && (transform.y <= maxY))
...
if ( Input.GetKey("down") && (transform.y >= minY))
...
if ( Input.GetKey("right") && (transform.x <= maxX))
...
if ( Input.GetKey("left") && (transforum.x >- minX))
Where minX, minY, maxX, and maxY are variables you defined and set.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to do camera scrolling? 1 Answer
Multiple Cars not working 1 Answer
Pinch zoom 0 Answers
Attaching an RTS scrolling script to a camera (Noob Question) 3 Answers