- Home /
door that closes by itself
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?
Answer by alexanderameye · Aug 24, 2017 at 03:57 PM
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!
https://www.assetstore.unity3d.com/en/#!/content/38694
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.
multiple doors using physics to open and close will harm the performance and please tell me 1 reason not to use Quaternions???
Could you please calm down.
The user requested a physics based approach ins$$anonymous$$d of animations. Assu$$anonymous$$g the user is creating a simple game it is very unlikely that he has a lot of doors in a single scene. I offered the user an approach using a trigger zone since that might provide some more flexibility than a timer, although a timer has less performance impact, a trigger zone certainly also has his benefit.
And why do you assume I am not using quaternions?
I didn't mean to be aggressive. sorry if I harassment you.
Answer by WinterboltGames · Aug 24, 2017 at 04:03 PM
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");
}
}
}
Your answer
Follow this Question
Related Questions
How often do Raycasts check for collisions? 1 Answer
How to Fix a Guns Firing Script so it Doesn't Constantly Fire 3 Answers
Diagonal Raycasting to detect platform returns true, even when false 1 Answer
camera.ScreenPointToRay always has same origin... 1 Answer
Why doesn't Physics.Raycast(ray, RaycastHit, distance) work? 1 Answer