- Home /
[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);
}
}
Answer by RabidCabbage · May 19, 2015 at 10:20 PM
Have you tried Input.GetMouseButton() ?
This constantly sends, instead of just once.
It seems to send exactly the same as Get$$anonymous$$ouseButtonDown. I fixed it by adding an extra bool but am marking this as right answer since no other has been added. Thanks!
Answer by ChosenVan · Nov 04, 2018 at 12:53 PM
@Eugenius thank you :) I took most part of your codes.
this part works for me :)
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);
}
}
Your answer
Follow this Question
Related Questions
2D rotation around hinge joint with joystick input 0 Answers
Based on touch rotate camera object 0 Answers
Space Camera Issue. 1 Answer