- Home /
This doormanager script from unity 3x Game Development Essentials is supposed to raycast the door open but it doesn't. Why not? Code now attached.
I have included two scripts I typed out from unity 3x game development essentials, that are supposed to demonstrate raycasting a door open only when the player object is facing it. Instead it seems to loop the sound of the door opening and closing very quickly, till it sounds like a radio with bad transmission, and the only animation you get to see is of the door closing when you stop facing it. Would someone please point out the script error(s) in the attachment - I want the door to open and close properly. Please note I have reimported all the assets, the console is error free, and all code is in javascript.
Thank you in advance
Thank you for pointing out attachments are not there. So following is the code of the door that is supposed to open and close the door once when triggered by a raycast modifier in another javascript:
private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0f;
var doorOpenTime : float = 3.0f;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
function Start() {
doorTimer = 0.0f;
}
function Update() {
if(doorIsOpen) {
doorTimer += Time.deltaTime;
if(doorTimer > doorOpenTime) {
Door(doorShutSound, false, "doorshut");
doorTimer = 0.0f;
}
}
}
function DoorCheck() {
if(!doorIsOpen) {
Door(doorOpenSound, true, "dooropen");
}
}
function Door(aClip : AudioClip, openCheck : boolean, animName : String) {
audio.PlayOneShot(aClip);
doorIsOpen = openCheck;
transform.parent.gameObject.animation.Play(animName);
}
And here is the raycast javascript that is supposed to trigger it once:
private var currentDoor : GameObject;
function Update() {
var hit : RaycastHit;
if(Physics.Raycast (transform.position, transform.forward, hit, 3)) {
if(hit.collider.gameObject.tag=="playerDoor"){
currentDoor = hit.collider.gameObject;
currentDoor.SendMessage("DoorCheck");
}
}
}
Answer by DuxDucis · Dec 19, 2012 at 11:40 PM
You're probably doing it each time the raycast is true, which will start the animation each frame you're looking at the door, so you should either try holding a boolean of whether the door is opening and check before making the raycast, or use animation.PlayQueued
The script has a door checking boolean function which is supposed to prevent the raycast from unceasingly triggering the door. It doesn't register console errors, but it just doesn't open and close the door once like it is supposed to. Can someone please point out the error in the code (now included above)?
Answer by Nubz · Dec 20, 2012 at 02:52 AM
Keep reading Will shows 3 ways to handle the door and the last one is the good one
If you still have the code from the Door$$anonymous$$anager javascript and the raycast door opening script in PlayerCollisions and it opened and closed the door properly - maybe you commented it out ins$$anonymous$$d of deleting it? - could you copy and paste it here so I could compare it with $$anonymous$$e and look for the error.
I did my scripts in C# and I only have the finished one saved
also looks like you said you made 2 scripts? This could be the problem since it's supposed to be on plus it looks like you are missing parts of it I would redo that chapter if I were you
Answer by ferbco · Dec 21, 2012 at 09:47 PM
error located. had to change the code a bit from what was shown in the book
Answer by willgoldstone · Dec 21, 2012 at 11:39 AM
Hi Ferbco, that's one heck of a headline! Not sure why you're seeing this. Despite the fact that your raycast occurs each frame as DuxDucis points out, the if(!doorIsOpen) flag should stop you doing this more than the first time, until that variable is reset.
Answer by Dave-Carlile · Dec 21, 2012 at 12:45 PM
Looking through the scripts it seems like they should work properly. While facing the door it should open, then close 3 seconds later, then repeat.
The doorOpenTime variable is controlling how fast the door opens and shuts. While the value is 3 in the script, if it was ever overidden in the editor then the editor value will take precedence - even if it was 0 when the script was first added to the gameobject, it will remain 0 even if you're changing the value in the script. So verify that the value shows as 3 in the editor. If it doesn't, right click on the component in the editor and select "reset", and it will grab the default values from the script again.
Not saying that is for sure the problem, but I've been bitten by that quite a few times, where I'm changing constants in a script to adjust things, but the editor keeps the values it had until I reset.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085650im_/https://answers.unity.com/themes/thub/images/avi.jpg)