How do I limit a door's rotation?
So, I am a complete beginner at Unity, but I'm definitely enjoying the learning process.
My goal at the moment is to create doors that open and close at the click of my mouse (in the simplest way possible), so that I can move on and start filling my world with collectible items. Right now I can open the door and I have it clamped so that it doesn't rotate more if I click it multiple times. The issue I've run into is that I can't figure out how to return the door to it's original position upon clicking it the second time. As it stands, when I click the door it opens, but then it becomes frozen in place until I exit Play Mode. Is there an easy fix to this problem? (And yes, I realize that I have some unnecessary bits left in the code, but I'll go back and clean it up once it is functional.) Thanks for taking a moment to help me out.
public class CabinetRightDoor : MonoBehaviour
{
public enum State
{
open, close
}
public State state;
private float minRotation = 90;
private float maxRotation = -90;
// Start is called before the first frame update
void Start()
{
state = CabinetRightDoor.State.close;
}
// Update is called once per frame
void Update()
{
}
public void OnMouseEnter()
{
Debug.Log("Enter");
}
public void OnMouseExit()
{
Debug.Log("Exit");
}
public void OnMouseDown()
{
Debug.Log("Click");
if (state == CabinetRightDoor.State.close)
Open();
if (state == CabinetRightDoor.State.open)
Close();
}
private void Open()
{
transform.Rotate(0, -90, 0);
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.y = Mathf.Clamp(currentRotation.y, minRotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
}
private void Close()
{
transform.Rotate(0, 90, 0);
}
}
Your answer
Follow this Question
Related Questions
Configurable joint don't rotate around the local axis of the rigidbody 0 Answers
how would I make a capsule rotate and go down a platform on the y axis? 0 Answers
Move an object around all 6 sides of a cube 2 Answers
How to use physics to make an object face a specific direction 0 Answers
How to get player (capsule) to tumble and NOT roll as a form of movement. 1 Answer