I’m trying make a door that opens with user input but then closes by itself a few second later using raycast NOT animation
Can anyone help?
I’m trying make a door that opens with user input but then closes by itself a few second later using raycast NOT animation
Can anyone help?
Hey! I have created this script for opening doors that you can download for free, it is well documented and is useful to read. It uses Raycasting to rotate a door!
For the automatic closing, you could create a trigger zone using a collider. On that trigger zone you then need to use a script that checks when the user has entered using the function ‘OnColliderEnter’ or ‘OnColliderStay’. Using that function you can send a ‘message’ to your door script saying that it has to close the door. That’s basically the method you could use to achieve the automatic closing of your door.
You could also use a timer instead of working with a trigger zone: see KillerOfSteam7’s answer.
the easiest method I could think of is simply using Quaternion.Euler(x, y, z)
and some coroutines like this…
public float DoorOpenAngle = 90f; // the value of the y-axis when the door is open
public float DoorClosedAngle = 0f; // the value of the y-axis when the door is closed
public bool IsOpen = false;
public IEnumerator AutoClose ()
{
yield return new WaitForSeconds (10f); // set it to whatever you like...
transform.rotation = Quaternion.Euler(0f, DoorClosedAngle, 0f);
}
private void SwitchDoor ()
{
if (!IsOpen)
{
var _openRotation = Quaternion.Euler(0f, DoorOpenAngle, 0f);
transform.rotation = _openRotation;
IsOpen = true;
StartCoroutine(AutoClose()); // call these here and the door will close after 10 seconds...
} else if (IsOpen)
{
var _closedRotation = Quaternion.Euler(0f, DoorClosedAngle, 0f);
transform.rotation = _closedRotation;
IsOpen = false;
}
}
then call it with raycasting as follows…
private void Update ()
{
var Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var Hit = new RaycastHit()
if (Physics.Raycast(Ray, out hit))
{
// you can do this with you own method of checking
if (Hit.transform.gameObject.tag == "Door")
{
Hit.transform.gameObject.SendMessage("SwitchDoor");
}
}
}