2
\$\begingroup\$

Why is the parent that contains 4 children jumping around and then settling into a position instead of rotating once by 90 deg? I can't seem to get it to rotate a single time as expected.

using UnityEngine;

public class rotateparent : MonoBehaviour
{
    public Vector3 rotationPoint;
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            transform.Rotate(new Vector3(0, 0, 1), 90);
        }
    }
}

Object jumping when mouse clicked

\$\endgroup\$
1
\$\begingroup\$

GetMouseButton(0) returns true every frame the mouse button is pressed. So if you hold the button for three frames, you get three rotations. This is very easy to do if your framerate is high, like in a nearly-empty scene like this.

To rotate just once per click, you should use GetMouseButtonDown(0) which returns true just once for each time the mouse button transitions from up to down, and not again until it's released and pressed a second time.

In future, you can diagnose this type of issue by adding a Debug.Log() to your code to print out a timestamp every time the condition is true, or using a breakpoint to step through each time this piece of code gets hit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.