Hi I am trying to spin a object with drag. For example a windmill I want the player to hold one of the blades and spin the windmill around and if they let go the windmill will continue to spin and eventually stop by the drag caused by the physics. I got a code below but unfortunately it stops soon as you let go of the object. I want the object to spin on the Z axis regardless of what direction the camera is looking. Please help thank you.
using UnityEngine;
using System.Collections;
public class SpinWithTouch : MonoBehaviour
{
private float baseAngle = 0.0f;
void OnMouseDown ()
{
Vector3 pos = Camera.main.WorldToScreenPoint (transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2 (pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2 (transform.right.y, transform.right.x) * Mathf.Rad2Deg;
}
void OnMouseDrag ()
{
Vector3 pos = Camera.main.WorldToScreenPoint (transform.position);
pos = Input.mousePosition - pos;
float ang = Mathf.Atan2 (pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
transform.rotation = Quaternion.AngleAxis (ang, Vector3.forward);
}
}