- Home /
How to link object.rotations to mouse.movements?
Hi guys, here u are my goal:
I have this cannon and I want to control its rotation on x axis moving the mouse up & down. But since I can't do that I decide to post here.
I am actually using a script that control the movement (with rotation limits processed by clamp method) pressing up arrow and down arrow.
But I would like to add the mouse function too.
Which is the best way?
Please answer in C# and if possible be clearer as u can because I am a true beginner. :(
If can help, the script I am using to rotate with arrows and limit the rotation of the cannon is the follow:
using UnityEngine;
using System.Collections;
public class ClampingRotation : MonoBehaviour
{
public float ySpeed = 250.0f;
public float yMinLimit = -80f;
public float yMaxLimit = 80f;
private float x = 0.0f;
private float y = 0.0f;
private float z = 0.0f;
void Start()
{
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;
z = transform.eulerAngles.z;
}
void Update()
{
if ( Input.GetKey(KeyCode.UpArrow) )
{
y += ySpeed * Time.deltaTime;
}
if ( Input.GetKey(KeyCode.DownArrow) )
{
y -= ySpeed * Time.deltaTime;
}
y = ClampAngle( y, yMinLimit, yMaxLimit );
Quaternion newRot = Quaternion.Euler( x, y, z );
transform.rotation = newRot;
}
float ClampAngle( float angle, float min, float max )
{
if ( angle < -360 )
angle += 360;
if ( angle > 360 )
angle -= 360;
return Mathf.Clamp( angle, min, max );
}
}
best regards to you all guys and I hope to hear from you soon!
Bye
Matthew
o yessss! thanks, it works!!! :))))) I feel somewhat so stupid when thinghs are so easy for other people and almost impossible for me!!!
Answer by AlucardJay · May 25, 2013 at 05:52 PM
Agreed. Change your arrow input to :
y += Input.GetAxis( "Mouse Y" ) * ySpeed * Time.deltaTime;