- Home /
Door only opening once on Raycast
I'm trying to get a door to open properly - using an animation, from both directions. At the moment, I've gotten it to open going one direction, but then it refuses to open again from the other. The script is very simple:
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.forward, out hit, 2.0F))
{
hitObject = hit.transform.parent;
string objectName = hitObject.name;
switch (objectName)
{
case "door_and_Frame":
hitObject.animation.Play("Take 001");
break;
}
}
Is there any recommended way to reset the raycast - or object, so that it detects it again coming through the door a second time or in the opposite direction?
Answer by Aram-Azhari · Feb 04, 2012 at 09:04 AM
If you are trying to open the door by getting close to it, then you should use colliders/triggers.
Physics.RayCast is usually used to detect the presence of an object in front of the ray.
But in you current case, after you open the door once, try to get away from the door with a long distance without looking at it. Then come back and see if it triggers again.
No go. Even through a door thath hasn't been opened yet and back again... and it does the same thing. The second door opens... and then locks you out.
I assume the code is on the player object. On which event are you using these code?
Update() C#, with the code placed on the player camera. I did some research before posting this and it seems that it only hits each object once.. and then does not reset for that object. The script is ment for interaction with other objects as well, not just doors which is why raycast was used.
Your answer
Follow this Question
Related Questions
C# raycast shooting script broken 1 Answer
C# - Trigger Animation with Raycasting? 1 Answer
Another Raycast Issue. 0 Answers
Need some help with to grap .x with a Raycasthit (c#) 1 Answer
Raycasting Help 1 Answer