[Unity5] Rotate UI button with mouse/finger

Hi everyone,

It’s been a while since I asked last but I’m in a bit of a pickle.

I’m wondering if there’s any easy way of rotating a new UI button with the mouse/tap gesture.

I’ve made up a script based on another answer found on the forum but it doesn’t seem to work - specifically because Input.GetMouseButtonDown(0) is true only upon click and not while the mouse button is actually down.

This is the script so far:

public class RotateScript : MonoBehaviour {

	private float rotationSpeed = 10.0f;
	private float lerpSpeed = 1.0f;

	private Vector3 theSpeed;
	private Vector3 avgSpeed;
	public bool isDragging = false;
	private Vector3 targetSpeedX;

	public Transform myObj;

	public void OnMouseDown()
	{
		isDragging = true;
	}
	
	// Update is called once per frame
	 void Update () 
	{

		if (isDragging && Input.GetMouseButtonDown(0)) 
		{
			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);
		}

		myObj = this.GetComponent<Transform> ();

		transform.Rotate (myObj.up * theSpeed.x * rotationSpeed, Space.World);
		transform.Rotate (myObj.right * theSpeed.x * rotationSpeed, Space.World);
	}
}

Have you tried Input.GetMouseButton() ?

This constantly sends, instead of just once.

@Eugenius thank you :slight_smile:
I took most part of your codes.

this part works for me :slight_smile:

public class ButtonRotate : MonoBehaviour {

	private float rotationSpeed = 10.0f;

	private Vector3 theSpeed;
	private Vector3 avgSpeed;

	public Transform myObj;

	void Update () 
	{

		if (Input.GetMouseButton(0)) 
		{
			theSpeed = new Vector3 (-Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y"));
			avgSpeed = Vector3.Lerp (avgSpeed, theSpeed, Time.deltaTime * 5);
		} else 
		{
			theSpeed = new Vector3 (0,0,0);
		}

		myObj = this.GetComponent<Transform> ();
		transform.Rotate (myObj.forward * theSpeed.x * rotationSpeed, Space.World);
	}
}