1

In my unity 3D project I have an Ground Plane Stage with a child object. While I press an UI-Button the object should rotate, if I release the button, the rotation should stop. Unfortunately it does not work. It only shows me my Debug.Log function but the object does not rotate.

Here is my script:

using System.Collections;
using System.Collections.Generic;
using Unity.Engine;

public class RotateCube : MonoBehaviour
{
    public Rigidbody rb;
    public rotateStatus = false;
    //public float rotationSpeed = 100f;

    public void rotateNow()
    {
        rotateStatus = !rotateStatus;
        Debug.Log("Rotation position = " + transform.eulerAngles.y)
    }

    void Update()
    {
        if(rotateStatus)
        {
             rb.transform.Rotate(0, 45, 0, Space.World);
             // Also tried it with;
             // rb.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
             Debug.Log("Rotation should been happend.");
        }
     }
}

The script is attached to my gameObject. The UI-Button has an OnClick Event with the gameObject and linked to the rotateNowfunction.

I tried also the Input.GetMouseButtonDown(0) and Input.GetButtonDown() method. Which would work, but with these methods everytime i press the screen my object will rotate.

This is what my console shows if I test it on the iPad:

(Filename: ./Runtime/Export/Debug.bindings.h Line 45)

Rotation should have been happened.

Edit

enter image description here

2
  • Where is the event attached to the UI-button press ? Issue could come from there Commented Sep 25, 2019 at 8:22
  • @Cid the script is attached to the gameObject and in the UI-button i have an OnClick() where the gameObject is attached .. see Edit. Commented Sep 25, 2019 at 8:48

1 Answer 1

3

this rotates the object about 45° every frame ... it is simply to fast for your eyes

You should use Time.deltaTime in order to convert the rotation into a smooth "rotation / second"

Rotate(0, 45 * Time.deltaTime, 0, Space.World);

Then as soon as a Rigidbody is involved you should not set any transformations through the Transform component but rather use RigidBody.MoveRotation in FixedUpdate in order to rotate the object but keept the physics intact

void FixedUpdate()
{
    if(rotateStatus)
    {
         rb.MoveRotation(rb.rotation * Quaternion.Euler(Vector3.up * rotationSpeed * Time.deltaTime));
         Debug.Log("Rotation should be happening.");
    }
 }

if i release the button, the rotation should stop

This will not happen. Unity's default button has no implementation for telling your scripts that the button is not pressed anymore.

You can write your own extension for this using the IPointerUpHandler and IPointerExitHandler interfaces:

public class ReleaseButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    public UnityEvent onPointerUp;

    [SerializeField] private Button button;

    private void Awake()
    {
        if(!button) button = GetComponent<Button>();
    }

    // according to the Docs this has to be implemented in order to
    // receive OnPointerUp events ... though we don'T need it actually
    public void OnPointerDown(PointerEventData pointerEventData){ }

    public void OnPointerExit(PointerEventData pointerEventData)
    {
        if(!button.interactable) return;

        onPointerUp.Invoke();
    }

    public void OnPointerUp(PointerEventData pointerEventData)
    {
        if(!button.interactable) return;

        onPointerUp.Invoke();
    }
}

attach this to your button and reference your RotateNow also in onPointerUp so the rotateStatus will be reset when releasing the button.

Sign up to request clarification or add additional context in comments.

7 Comments

I tried it with void FixedUpdate but unfortunately there is still no rotation. only the Debug.Log works.
Can you make sure the rotationSpeed is not 0 in the Inspector? Oh and the Rigidbody component should ofcourse be enabled but I would assume that
If I test the script in Unity it works fine, i can see that the object is rotating and the inspector shows it, but if I test it on my iPad, the object is not rotating. Even if i Debug.Log the rotation position it show that the rotation-numbers go up, but the object does not rotate.
Do you maybe have the rotation constrained on the Rigidbody or is it possible that anything else interferes with its orientation?
Where can i see if the rotation is constrained? I am new to unity and c#.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.