- Home /
How can I rotate around the local z axis based off starting location in relation to where the mouse is dragged?
I am trying to rotate a circle around the it's local z axis, but I need it to rotate toward where the mouse is currently dragged to.
I am using NGUI to handle the capture of the drag event. The problem is over looking at all the different methods to handle rotation the closest thing I can find is Quaternion.FromToRotation. The problem here is that I don't know how to capture the start location as every time the circle moves the start rotation needs to move.
The following code "sort of" works in that I can rotate the circle toward where the mouse is, but I have to make a really large circle with the mouse drag around the outter edges of the object. I know this is because the start location is static to where I clicked on the object. Where it seems that after the initial click this start location needs to be updated to the previous location of where the drag took place?
Also, there is the obvious problem that whenever the drag stops and the click starts again it seems to reset the circle.
I am new to coding in 3D, so I am still trying to wrap my head around vectors, Quaternion , eulers and all that.
using UnityEngine;
public class SpinWithMouse : MonoBehaviour
{
public Transform target;
public float speed = 1f;
Transform mTrans;
Vector3 currentLoc;
public float angle = 0f;
void Start ()
{
//Set the transform for the script to the transform of the object the script is attached to
mTrans = transform;
}
void OnPress()
{
//Set the x,y,z that is currently clicked or touched
currentLoc = UICamera.currentTouch.pos;
}
void OnDrag (Vector2 delta)
{
//Set the x,y,z of where the mouse is currently at
Vector3 newLoc = UICamera.currentTouch.pos;
//Get the difference between the start angle and the stop angle
//Quaternion targetRotation = Quaternion.LookRotation(newLoc - currentLoc);
//Set the rotation of the object based on where the click occured and where the mouse is dragged i
mTrans.localRotation = Quaternion.FromToRotation(currentLoc,newLoc); //Quaternion.Euler(0f, 0f, -0.5f * delta.x * speed) * mTrans.localRotation;
Debug.Log("Position " + UICamera.currentTouch.pos);
Debug.Log("Wheel location " + mTrans.localRotation);
}
}
Correct me if I am wrong, but it seems like what I need to find is the angle or A, where as now I am actually comparing the angle of B to the angle of C? If I find the angle of A then I could essentially assign that to the Z of the Euler and get it to rotate correctly?
Answer by sparkzbarca · Oct 16, 2012 at 03:08 AM
oh wow thats only a 2d rotation around the Z axis super easy
you want to rotate by the change in the mouse position
the mouse position can be gotten by using
input.mouseposition
an angle needs 2 vectors to find an angle between
the first vector is the mouse position last frame
the second is the one this frame
input.mouseposition gives us the position of the mouse right now
input.getaxis("Mouse Y")
input.getaxis("Mouse X")
gives us a number showing how much change there was between this frame and the last.
basically its position last frame was its current position minus that change.
so
input.mouseposition is a vector2
old_pos = input.mousepostion - new vector2(input.getaxis("Mouse X"), input.getaxis("Mouse Y");
now we have the old position and the new is just input.mouseposition. now we need the angle between them. but there's a function for that
vector2.angle
vector2.angle(from, to)
vecto2.angle(oldpos,newposition)
now we rotate around the z axis angle degrees
wheel.transform.rotate(axis, angle to rotate)
wheel.transform.rotate(vector3.forward, angle)
all good?
This still doesn't get me where I need. All this seems to do is rotate the wheel in counter clockwise direction while I click and drag the mouse.
However, I need it to rotate in either clockwise or counterclockwise direction toward where the mouse is currently located. Almost something like the Quaternion.LookRotation but I haven't been able to get that to work.
Here is the code I am using currently where it just rotates as I move the mouse:
using UnityEngine;
public class SpinWith$$anonymous$$ouse : $$anonymous$$onoBehaviour { public float speed = 5f;
Transform mTrans;
Vector3 currentLoc;
void Start ()
{
//Set the transform for the script to the transform of the object the script is attached to
mTrans = transform; }
void OnPress()
{
//Get the distance of x,y,z from the center currentLoc = Input.mousePosition - new Vector3(Input.GetAxis("$$anonymous$$ouse X"), Input.GetAxis("$$anonymous$$ouse Y")); }
void OnDrag (Vector2 delta)
{
//Set the x,y,z of where the mouse is currently at
Vector3 newLoc = Input.mousePosition; //Get the distance of x,y,z from the center float angle = Vector3.Angle(currentLoc,newLoc); mTrans.transform.Rotate(Vector3.forward, angle); Debug.Log("Wheel location " + mTrans.localPosition); Debug.Log("Clicked location " + currentLoc); Debug.Log("Dragged location " + newLoc); } }
Answer by sparkzbarca · Oct 14, 2012 at 09:10 PM
your not trying to rotate around the local Z axis I dont think.
I think your trying to rotate around the axis parallel to the mouse
like this
Am I correct in thinking thats what you want. For the object to spin in place so that it spins from the top of the sphere towards you and then rotates back down?
if thats what you need let me know and i can post some help in doing it just want to make sure I got the problem right first.
I am basically trying to turn the wheel around in a circle, where it rotate around the z-axis.
Here is what I am actually working on: http://www.thegeekenders.com/resources/misc/Zpoc/WebPlayer.html
Have you solved the problem? If yes, could you post the code. Thank you.
Your answer

Follow this Question
Related Questions
How to increase the range of the player aiming? 2 Answers
controller 2 Answers
Authoritative Rotation 2 Answers
Mouse Look from scratch rotating Z axis 2 Answers
Rotation of Object on single axis in direction of the mouse position 0 Answers