- Home /
Move camera when right mouse button is pressed
Hi guys im new to scripting and i need help i want to make the camera rotate around the Y-axis when i press the right mouse button and when released it stays still can some one plz help.
Thanks
Answer by diabloroxx · Dec 13, 2010 at 09:37 PM
Check this for Rotating an object: http://unity3d.com/support/documentation/ScriptReference/Transform.Rotate.html
Check this for getting Input and identifying them:
Try this:
void Update()
{
if ( Input.GetMouseButton( 1 ) )
{
Camera.mainCamera.transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
}
}
Answer by skovacs1 · Dec 13, 2010 at 09:33 PM
You say "rotate around the Y-axis", but you don't say which Y-axis
Local y-axis rotation:
RightMouseRotate.js (Attached to camera);
var speed : float = 5.0f;
function Update() { if(Input.GetMouseButton(1)) transform.eulerAngles.y += Input.GetAxis("Mouse Y") * speed; }
World y-axis rotation:
RightMouseRotate.js (Attached to camera);
var speed : float = 5.0f;
function Update() { if(Input.GetMouseButton(1)) transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse Y") * speed); }
You also don't specify how you want it to move. The above was based on mouse input. The following is automated movement:
Local y-axis rotation:
RightMouseRotate.js (Attached to camera);
var speed : float = 30.0f;
function Update() { if(Input.GetMouseButton(1)) transform.eulerAngles.y += Time.deltaTime * speed; }
World y-axis rotation:
RightMouseRotate.js (Attached to camera);
var speed : float = 30.0f;
function Update() { if(Input.GetMouseButton(1)) transform.RotateAround(Vector3.zero, Vector3.up, Time.deltaTime * speed); }
Answer by Mickydtron · Dec 13, 2010 at 10:13 PM
Regardless of which Y axis we want to rotate around, transform.Rotate is the way to go. Either use
transform.Rotate(speed*Time.deltaTime);
or
transform.Rotate(speed*Time.deltaTime, Space.World);
for local or global rotation, respectively. Or for rotating around something, use what diabloroxx said, because that individual is an intelligent individual.
Your answer
Follow this Question
Related Questions
Move camera with mouse (2D) 1 Answer
How to get Mouse input using the new Unity Input System 2 Answers
Input.mousePosition Dead Zone - See gameplay 0 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