- Home /
Pan Camera with mouse?
I want to pan my orthographic camera with my mouse. I only want it to move X and Y. I want Z to stay the same. I script in javascript. Any ideas on how to do this? Thanks in advanced.
Answer by ncallaway · Jan 11, 2014 at 10:44 PM
public var mouseSensitivity : float = 1.0;
private var lastPosition : Vector3;
function Update()
{
if (Input.GetMouseButtonDown(0))
{
lastPosition = Input.mousePosition;
}
if (Input.GetMouseButton(0))
{
var delta : Vector3 = Input.mousePosition - lastPosition;
transform.Translate(delta.x * mouseSensitivity, delta.y * mouseSensitivity, 0);
lastPosition = Input.mousePosition;
}
}
I used a value of mouseSensitivity around 0.01f. This will definitely need to vary with the scale of your scene.
Thanks! By chance do you happen to know how to only move it within a certain area? Or restrict it from moving too far?
You would need to have a variable capture the camera's position on Start().
Then, after moving it in Update(), you could find the delta between the camera's current position, and where it was on Start(), then clamp that distance to whatever distance you want.
Alternatively, you could have maximum distances on the x and y axis independently, creating a rectangle-shape ins$$anonymous$$d of a circle-shape.
That speed was more than adequate, you can replace the Input.mousePosition with Camera.main.ScreenToWorldPoint(Input.mousePosition)
Your answer
Follow this Question
Related Questions
Pan Orthographic Camera? 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Pan Orthographic Camera 0 Answers