- Home /
spinning an object with mouse movement
Hey folks, I want to be able to swipe my mouse on the screen in a certain direction and have a floating on-screen cube rotate in a that direction, kind of in the same way it would look if the cube was static and the camera were to orbit around the cube - I want the cube to spin in the direction of mouse movement, basically.
for another example of what I'm trying to get at, think of what happens when you go to rotate an object in the editor with the rotation gizmo. click on the gizmo, but not on a specific axis -- between the axis, the object rotates in the direction of your mouse. that's the effect I want, but without the gizmo...
Anyone have any ideas?
Thanks!
Answer by Stardog · Jan 29, 2012 at 08:16 AM
I made this for picking up and rotating physics objects for an adventure game, but I've stripped it down. Mouse X/Y should be in Project Settings > Input by default.
using UnityEngine;
using System.Collections;
public class ObjectRotate : MonoBehaviour {
public float multiplier = 10f;
void OnMouseDrag()
{
transform.Rotate(Input.GetAxis("Mouse Y") * multiplier, -Input.GetAxis("Mouse X") * multiplier, 0, Space.World);
}
}
Thank you! that did it!
ugh, I'm an idiot. I had tried a similar thing at first, but I was using Input.GetAxis and I had made a stupid mistake setting up my axis in the input manager. I thought it was getting a bad result because of something regarding Quaternions or something.
Your answer
Follow this Question
Related Questions
How to rotate an object around a fixed point so it follows the mouse cursor? 1 Answer
How to make an object rotate on the y axis towards the mouse? 3 Answers
Rotate object following mouse movement. Object jumps/ flips 1 Answer
Rotation of the mouse activates a boxcollider2d in javascript. 1 Answer
i want to rotate an object when i move my mouse over an UI image 1 Answer