Spin a 2D object with mouse drag

Hello everyone,

I am trying to have a control like in the game in this youtube video and so far i am having issues with transform.rotate, i tried to look on similar questions and so far this is what i’ve got :

using UnityEngine;
using System.Collections;

public class DragRotateSlowDown : MonoBehaviour
{
	
		private float rotationSpeed = 10.0F;
		private float lerpSpeed = 1.0F;
		private Vector3 theSpeed;
		private Vector3 avgSpeed;
		private bool isDragging = false;

		void OnMouseDown ()
		{
		
				isDragging = true;
		}
	
		void Update ()
		{
		
				if (Input.GetMouseButton (0) && isDragging) {
						theSpeed = new Vector3 (-Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y"), 0.0F);
						avgSpeed = Vector3.Lerp (avgSpeed, theSpeed, Time.deltaTime * 5);
				} else {
						if (isDragging) {
								theSpeed = avgSpeed;
								isDragging = false;
						}
						float i = Time.deltaTime * lerpSpeed;
						theSpeed = Vector3.Lerp (theSpeed, Vector3.zero, i);
				}
		
				transform.Rotate (Camera.main.transform.forward * theSpeed.x * rotationSpeed, Space.World);
				transform.Rotate (Camera.main.transform.forward * theSpeed.y * rotationSpeed, Space.World);

		}
}

but there is a lot of problems with this solution, mainly, the object doesn’t continue to rotate in a certain moment, it “stop” following the mouse and instead it rotate in the opposite direction.

Any help will be heavily appreciate it,
thank you all and have a great day.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour 
{
	public Transform cube;

	float deltaRotation;
	float previousRotation;
	float currentRotation;

	void Start () 
	{
	
	}
	
	void Update () 
	{
		if (Input.GetMouseButtonDown (0))
		{
			deltaRotation = 0f;
			previousRotation = angleBetweenPoints( cube.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
		}
		else if(Input.GetMouseButton(0))
		{
			currentRotation = angleBetweenPoints( cube.position, Camera.main.ScreenToWorldPoint(Input.mousePosition) );
			deltaRotation = Mathf.DeltaAngle( currentRotation, previousRotation );
			previousRotation = currentRotation;
			cube.Rotate( Vector3.back, deltaRotation );
		}

	}

	float angleBetweenPoints( Vector2 position1, Vector2 position2 )
	{
		var fromLine = position2 - position1;
		var toLine = new Vector2( 1, 0 );
		
		var angle = Vector2.Angle( fromLine, toLine );
		var cross = Vector3.Cross( fromLine, toLine );
		
		// did we wrap around?
		if( cross.z > 0 )
			angle = 360f - angle;
		
		return angle;
	}
}

adapted from TouchKit - TKOneFingerRotationRecognizer

thnx itsa for your help, here is the final code with the wanted effect :slight_smile:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
		public float deltaRotation;
		public float deltaLimit;
		public float deltaReduce ;
		float previousRotation;
		float currentRotation;

		void Start ()
		{
				Screen.showCursor = false;
		}
	
		void Update ()
		{
				if (Input.GetMouseButtonDown (0)) {
						deltaRotation = 0f;
						previousRotation = angleBetweenPoints (transform.position, Camera.main.ScreenToWorldPoint (Input.mousePosition));
				} else if (Input.GetMouseButton (0)) {
						currentRotation = angleBetweenPoints (transform.position, Camera.main.ScreenToWorldPoint (Input.mousePosition));
						deltaRotation = Mathf.DeltaAngle (currentRotation, previousRotation);
						if (Mathf.Abs (deltaRotation) > deltaLimit) {
								deltaRotation = deltaLimit * Mathf.Sign (deltaRotation);
						}
						previousRotation = currentRotation;
						transform.Rotate (Vector3.back * Time.deltaTime, deltaRotation);
				} else {
						transform.Rotate (Vector3.back * Time.deltaTime, deltaRotation);
						deltaRotation = Mathf.Lerp (deltaRotation, 0, deltaReduce * Time.deltaTime);
				}
				
		}
	
		float angleBetweenPoints (Vector2 position1, Vector2 position2)
		{
				Vector2 fromLine = position2 - position1;
				Vector2 toLine = new Vector2 (1, 0);
		
				float angle = Vector2.Angle (fromLine, toLine);
				
				Vector3 cross = Vector3.Cross (fromLine, toLine);
				
				// did we wrap around?
				if (cross.z > 0) {
						angle = 360f - angle;
				}
		
				return angle;
		}

		
}