Okay I’ve created a script that should open and close a door when you’re in range.
Problem is the script is not working how I would have expected. Here’s the script:
var Player : Transform; //player position
var handle : Transform; //door handle position
var Range : float;
var openstate : float;
function Update ()
{
** var dist : float = Vector3.Distance(Player.position, handle.position);**
if(dist <= Range)
** {**
** if (Input.GetKey(“mouse 0”))**
** { if(openstate == 0)**
** {**
** OpenDoor();**
** } else if (openstate == 1) {CloseDoor();}**
** } **
** }**
}
function OpenDoor()
{
animation.Play(“Open”);
openstate =1;
}
function CloseDoor()
{
animation.Play(“Close”);
openstate =0;
}
The script assumes you have two animations setup (Open Close) The script added to the door. The idea is once the player is in range they can just click and the door opens, trouble is the door only plays the closed motion??? If I delete the “else” option the door opens fine.
I’m very confused???
I’ve attached a demo scene in the zip file.
Any help would be great,
Regards,
Shaun.
606602–21591–$Door.zip (216 KB)
Just guessing, but (at least some of) your problem may be caused by your use of Input.GetKey, which will continue to fire as long as the key is held. That’s likely causing multiple Open/Close cycles to fire in rapid succession.
What happens if you use Input.GetKeyDown instead?
Jeff
Hello!
I saw your script and found many errors there ( You can make it much simple)
Anyway, i rewrited that script, now you can open and close door by pressing “F” without using animations.
Here is the project with new script.
606827–21600–$Door (Rewrited).rar (195 KB)
-NSDG
Thanks to the both of you. Input.GetKeyDown works really well.
Thanks for the script NSDG, helped a lot with my own scripting. It’s been years, in fact I use to use a mix of C 68000 ASM back in the Amiga days. :0) So a little more catch up is needed. The Boolean function for a start is new to me. 
Worked out what your code did, and adjusted it so it can be used to trigger animations sounds. The door opening is just a step for a more advanced script. Start simple then work up. :0)
Any way a big thanks, I thought there was some kind fast looping going on with the Input.GetKey function, just was unsure how to fix it.
Regards,
Shaun.