- Home /
How to rotate an object's X, Y, Z based on mouse movement
Hi there. I'm having a hard time coming up with a script that will allow me to rotate an object by all three axis based on the direction I move my mouse. To clarify a little more for example: no matter what position on it's X, Y, and Z it is in, I'd like the object in to rotate downward and toward me upon moving the mouse downward, rotate leftward no matter it's X, Y, Z position if i move my mouse leftward, ETC.
Edit: I worked around this problem by using physics to my advantage: Two positions to apply force makes the object spin as if my mouse is controlling it. Here's the script. If there are no better answers in the next few days I'll close this as solved.
function Update () { rigidbody.AddForceAtPosition(Vector3(transform.position.x+(10*Input.GetAxis("Mouse X")),transform.position.y,transform.position.z+(10*Input.GetAxis("Mouse Y"))),Vector3(transform.position.x,transform.position.y+4,transform.position.z)); print(transform.rotation); }
Answer by flaminghairball · Jan 21, 2010 at 10:19 PM
Why not just use transform.Rotate()?
var speed : float = 1.0; //how fast the object should rotate
function Update(){
transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
}
Best wishes, -Lincoln Green
Is there away to put a restraint on this so it will only rotate a certain amount??
I'd guess you would have to keep track of the current rotation, or using transform's quaternion to check, and then do or not do the rotation.
have u tried an attribute? http://unity3d.com/learn/tutorials/modules/intermediate/scripting/attributes
It says Unexpected symbol 'Time' ??? what is the problem?
Answer by TimTheTitan · Feb 12, 2016 at 01:54 PM
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
Hi folks,
the snippet from TimTheTitan works well so far. But i want to add some enhancements to it. i tried a little bit but my skills are to low ^^
the rotation should set itself back to 0,0,0 after 10 Seconds if theres no new input for this time. ( i do the rotation On$$anonymous$$ouseButton (0) and if after 10seconds no new input is given, it should set it back to zero) hope some1 understands me :P
i want to give a maximum for the Y Axis, lets say 25degree
and last but not least, the translation should be stopping smoothly after releasing the $$anonymous$$ouseButton.
I know its not Christmas but if some1 has an awnser or solution to it, it would be nice to read it. sry for my "bad" english :D
greets
Hi Rene T,
This is what you could do. I didn't test it, because I am busy, but it should work. You only have to reset and limit the rotation yourself. (Check links).
//your current time
private float currentTime = 0.0f;
//your timer limit
private float timerLimit = 10.0f;
void Update()
{
//If there is no input from the left mouse button
if(!Input.Get$$anonymous$$ouseButton(0))
UpdateTimer();
if(currentTime >= timerLimit)
{
//reset your rotation here
currentTime = 0.0f;
}
}
void UpdateTimer()
{
CurrentTime += Time.deltaTime;
}
You can find most of the things yourself pretty easily.
Some useful links:
Tim
Answer by BloodK · Aug 01, 2011 at 12:26 PM
float yawMouse = Input.GetAxis("Mouse X");
float pitchMouse = Input.GetAxis("Mouse Y");
Vector3 targetFlyRotation = Vector3.zero;
Screen.lockCursor = true;
if (Mathf.Abs(yawMouse) > 0.1f || Mathf.Abs(pitchMouse) > 0.1f)
{
targetFlyRotation = yawMouse * transform.right + pitchMouse * transform.up;
targetFlyRotation.Normalize();
targetFlyRotation *= Time.deltaTime * 3.0f;
//limit x rotation if looking too much up or down
//Log out the limitX value for this to make sense
float limitX = Quaternion.LookRotation(moveDirection + targetFlyRotation).eulerAngles.x;
//70 sets the rotation limit in the down direction
//290 sets limit for up direction
if (limitX < 90 && limitX > 70 || limitX > 270 && limitX < 290)
{
Debug.Log("restrict motion");
}
else
{
moveDirection += targetFlyRotation;
//does the actual rotation on the object if no limits are breached
transform.rotation = Quaternion.LookRotation(moveDirection);
}
}
Answer by RT3DViz · Apr 20, 2016 at 03:01 PM
Hi and thx Tim,
may i ask u something to the suggested solution? i tried but didnt get it :(
heres my code so fare: public GameObject Target; public float horizontalSpeed = 2.0F; public float verticalSpeed = 2.0F; public float angle = 45.0f;
void Update()
{
if (Input.GetMouseButton(0))
{
MouseSpin();
}
if (Input.GetMouseButtonDown(1))
{
Target.transform.rotation = Quaternion.identity;
}
}
void MouseSpin()
{
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
v = Mathf.Clamp(v, 1, -1);
Target.transform.Rotate(v, h, 0);
}
}
the clamp doesnt work ^^ maybe im just to dumb, what did i wrong?
greets
You shouldn't post a question as an answer. If you can't find any similiar questions on Unity Answers you should make a new topic. Then you might get some more replies.
Try to explain what doesn't work. Did you get an error? And try to debug with http://docs.unity3d.com/ScriptReference/Debug.Log.html I haven't tested your code yet, so I am not sure what's going wrong. Timthe clamp doesnt work ^^
Your answer
Follow this Question
Related Questions
Rotating Sphere With Mouse 2 Answers
Lock rotation on Z Axis 1 Answer
eulerAngles.y always returns 179.9999 1 Answer
Rotating, stopping, rotating again. 2 Answers