Search Unity

How to rotate object on button click?

Discussion in 'Scripting' started by polan31, Sep 7, 2019.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    UNITY 2D C#

    I would like rotate my object when I hold the button that I created in canvas (OnClick method, NOT MOUSE BUTTON). Now when I hold the button, it only rotates per one frame. When I hold the button, the object should rotate until I release it. I tried to use bool, but it doesn't work, so I went back to the beginning. Any help?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Rotation : MonoBehaviour
    7. {
    8.  
    9.     public float rotateSpeed = 180f;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.     }
    16.  
    17.     void Update () {
    18.  
    19.     }
    20.  
    21.     public void OnClickLeft () {
    22.        
    23.         transform.Rotate(Vector3.forward*rotateSpeed*Time.deltaTime);
    24.  
    25.     }
    26.    
    27. }
    28.  
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Take a look at using an EventTrigger.

    Add an EventTrigger to your button and add the "Pointer Down" and "Pointer Up" event types to it.

    In your code, create a bool "isPressed":
    Code (CSharp):
    1. bool isPressed;
    Add a new function to toggle "isPressed":
    Code (CSharp):
    1. public void TogglePressed(bool value)
    2. {
    3.     isPressed = value;
    4. }
    In Update() check if "isPressed" is true and rotate the transform:
    Code (CSharp):
    1. void Update()
    2. {
    3.     if(isPressed)
    4.     {
    5.         OnClickLeft(); //or just move the rotation code here
    6.     }
    7. }
    Lastly, link the "TogglePressed" function to the "Pointer Down" event and check the checkbox. Do the same for "Pointer Up" event, but leave the checkbox unchecked.

    Now the object will rotate for as long as the button is pressed.
     
    polan31 likes this.
  3. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149

    I have it. Honestly, I didn't even realize that you could add the Even Trigger option to the buttons :)
     
    Last edited: Sep 7, 2019
  4. Refzlund

    Refzlund

    Joined:
    Oct 15, 2016
    Posts:
    11
    Take a look at IPointerDownHandler for EventSystems

    Attach this to the button and attach the GameObject you want to rotate to the rotatedObject variable.
    Here's a suggested solution:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. public class RotateObject : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    4. {
    5.     public GameObject rotatedObject;
    6.     public float rotationSpeed = 20;
    7.     bool rotate = false;
    8.  
    9.     void FixedUpdate()
    10.     {
    11.         if (rotate == false)
    12.             return;
    13.  
    14.         rotatedObject.transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
    15.     }
    16.  
    17.     public void OnPointerDown(PointerEventData pointerEventData)
    18.     {
    19.         rotate = true;
    20.     }
    21.  
    22.     public void OnPointerUp(PointerEventData pointerEventData)
    23.     {
    24.         rotate = false;
    25.     }
    26. }
     
    Last edited: Sep 7, 2019