public GameObject doorHinge;
public bool doorIsOpen;
// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.E))
{
doorHinge.transform.Rotate(0.0f, -90f, 0.0f);
doorIsOpen = true;
}
if (Input.GetKeyUp(KeyCode.E) && (doorIsOpen == true))
{
doorHinge.transform.Rotate(0.0f, 0.0f, 0.0f);
doorIsOpen = false;
}
}
I have very basic code. The door opens as it should after I press E with the correct rotation, but instead of going back to its default position, it turns -90 degrees 4 times.
That's why I put the bool there so shouldn't that prevent the door from turning the wrong way?
It seems like you have a fundamental misunderstanding of what the Rotate function does.
doorHinge.transform.Rotate(0.0f, 0.0f, 0.0f);
The above code is similar to the +=
assignment. Which means you say rotation += 0
. If you want to set the rotation value, then you should use rotation
instead of Rotate
To fix your code, you should have
doorHinge.transform.Rotate(0.0f, 90f, 0.0f);
or
doorHinge.transform.rotation = Quaternion.Identity;
You might benefit from watching some free classes on Unity Learn or using google more aggressively.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.