- Home /
Move camera with mouse (2D)
I want to create a map of my 3D scene. So I have placed a camera in the sky looking down. When M is pressed, I'm switching from the character controller camera to this camera. Now I want to move this camera in up, down, left and right directions with mouse's movement. So that would be the camera's movement in X and Z axes reading input from mouse's X and Z axes. Can you help me with the script? Thanks.
This is what I have so far:
if (Input.GetAxis("Mouse X")>0)
{
transform.localPosition = new Vector3 (transform.position.x,transform.position.y,transform.position.z+5);
}
if (Input.GetAxis("Mouse X")<0)
{
transform.localPosition = new Vector3 (transform.position.x,transform.position.y,transform.position.z-5);
}
if (Input.GetAxis("Mouse Y")<0)
{
transform.localPosition = new Vector3 (transform.position.x+5,transform.position.y,transform.position.z);
}
if (Input.GetAxis("Mouse Y")>0)
{
transform.localPosition = new Vector3 (transform.position.x-5,transform.position.y,transform.position.z);
}
But the movement is jagged especially when moved in diagonal directions. So should I consider input from both mouse axes at the same time?
Also how do I set bounds for the camera so it won't go off the terrain?
I just had to keep the rest of the C# code (using unity engine, update, etc...).
Answer by calmcarrots · May 11, 2014 at 06:45 PM
float speed = 3f;
transform.position += new Vector3(Input.GetAxisRaw("Mouse X") * Time.deltaTime * speed, 0f, Input.GetAxisRaw("Mouse Y") * Time.deltaTime * speed);
anything on how to set bounds for the camera so it won't go off the terrain?
transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -10f, 10f), transform.position.y, $$anonymous$$athf.Clamp(transform.position.z, -10f, 10f));
Change the '-10f' and '10f' to different numbers in order to limit the position to that number. Remember to check best answer :)
BOTH YOUR SCRIPTS ARE GIVING $$anonymous$$E PARSING ERRORS WTF!?
@$$anonymous$$igmaJet$$anonymous$$oto the code is in C#. You might be writing in Unityscript.
Your answer
Follow this Question
Related Questions
how to create a old (2D style) fps cursor 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Camera Screen to world point returns cameras transform position. 8 Answers
how can i instantiate a GameObject in mouse position and in a special distance from the camera? 2 Answers
using left right keys to turn 1 Answer