Hi guys, my name is Adam and I am relatively new to the wonderful world of Unity
I’m hoping one of you lovely people will be able to help me out with a scripting problem that I can’t seem to solve by myself (I have looked through the posts regarding opening sliding doors etc but nothing seems to address the issue I have…)
I have managed to attach the following script to open the door and trigger a sound when my third person character enters the trigger zone but instead of it opening and closing just once, it triggers multiple times and the door stutters, as does the sound.
How would I go about editing the script so that the door opens once when my player enters the trigger box and stays open until the player exits the box?
When you have such small scripts, it’s much easier to put it directly in your post, in code tags. Click the insert icon above the post input field (just to the left of the save icon) to get one that gives you the correct highlighting for your language.
As for the cause for why the door stutters… You haven’t by any chance made the trigger a child of the door? That will cause the trigger zone to move with the door, which is not something you want. Since you’ll probably be making a prefab of the door, I’d recommend a setup where the door is a child of the trigger zone.
In addition, you’ve set your door to move the distance it needs to move in one second, regardless of how far it has to move. This means that if the door starts opening while it’s closing, it will move slower than if it started opening from a completely closed state. I’d recommend your MoveDoor Coroutine with Vector3.MoveTowards:
Amazing, that’s helped me a great deal with the stuttering of the door, thank you ever so much!
I have come across a new hiccup now… When I go through the door, the door open sound plays as the door opens and then again when I go through the door?
Nor does the door close sound play when I exit the area?