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);


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


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.

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(from, to)


now we rotate around the z axis angle degrees

wheel.transform.rotate(axis, angle to rotate)

wheel.transform.rotate(vector3.forward, angle)

all good?

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?