- Home /
How to do camera scrolling?
I would like to make a 2D scrollable map using camera scrolling, however I have no idea on how should I go about doing it. Can anyone help me with this? Thanks.
Comment
Doing some tutorials will be very helpful for you if you have no idea how to at least do it.
Best Answer
Answer by LyanApps · Apr 02, 2013 at 04:41 PM
I assume your map is on the x,z plane.
float pan_speed = 10.0f;
function Awake(){
camera.y = 100; //put the camera above the world
camera.Rotate(transform.right, 90); // transform the camera on
}
function Update(){
if(Input.GetKey("up"))
camera.Translate(Vector3.forward * Time.deltaTime * pan_speed, Space.World);
if(Input.GetKey("down"))
camera.Translate(-Vector3.forward * Time.deltaTime * pan_speed, Space.World);
if(Input.GetKey("left"))
camera.Translate(-Vector3.right * Time.deltaTime * pan_speed, Space.World);
if(Input.GetKey("right"))
camera.Translate(Vector3.right * Time.deltaTime * pan_speed, Space.World);
}
Thanks a bunch, @LyanApps ! I changed a little bit of the codes for the "up" key and "down" key and placed them inside void OnGUI() though and it works (:
void OnGUI ()
{
if ( Input.Get$$anonymous$$ey("up") )
{
Camera.main.transform.Translate( Vector3.up * Time.deltaTime * scrollSpeed, Space.World );
}
if ( Input.Get$$anonymous$$ey("down") )
{
Camera.main.transform.Translate( Vector3.down * Time.deltaTime * scrollSpeed, Space.World );
}
}
Is it possible to set a limit as to how much the camera can be scrolled? And how do I go about doing it?