Game Development Asked on December 18, 2021
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);
}
}
}
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.
Answered by DMGregory on December 18, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP