How to calculate angles and apply to rotate an object.

I have an object that is a circle. I want to be able to rotate the circle around it’s center point which will always stay the same (currently I have it at 0,0,0).

I can get two other points in space, basically a start point (x1,y1) and then an end point (x2,y2) based off of where the mouse is clicked and then where the mouse is dragged to.

From this I can get the distance from the the center to the start and end point and from the stop to the end point to get a triangle. The problem from there is I’m not sure how I can calculate the angles.

If I am thinking this through correctly I need to get the angles of the start point and end point and then use those to derive the angle of the center point. Then somehow set the rotation of the object to the angle of the center point.

I have a little diagram below. I’m very dusty on my trig and still learning the Unity language so any help would be much appreciated!

Here is the code I have so far, I’m stuck at how to get all three angles and then what I should use to apply the angle to a rotation once I find it.

using UnityEngine;

public class SpinWithMouse : MonoBehaviour
{
    public float speed = 5f;

    Transform mTrans;
    Vector3 currentLoc;
	Vector3 startTouch;

    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
		startTouch = UICamera.currentTouch.pos;
		
		//Get the distance of x,y,z from the center
		currentLoc = startTouch - transform.localPosition;		
    }

    void OnDrag (Vector2 delta)
    {
        //Set the x,y,z of where the mouse is currently at
		Vector3 dragTouch = UICamera.currentTouch.pos;
		
		//Get the distance of x,y,z from the center
		Vector3 newLoc = dragTouch - transform.localPosition;
		
		Debug.Log("Wheel location " + mTrans.localPosition);
		Debug.Log("Clicked location " + currentLoc);
		Debug.Log("Dragged location " + newLoc);
		Debug.Log("Start Touch " + startTouch);
		Debug.Log("Drag Touch " + dragTouch);
    }
}

4205-rotate.png

this is a 2d rotation?

then you want to rotate around the y axis
angle degrees

angle can be found using the angle function

angle = vector2.angle(from angle(or first click pos), to angle(second click)

rotate(vector3.up, angle)

if good mark as answered if you need more help comment.