Door opening and closing script not working

I have this script attached to my door:

var InRange : boolean = false;
var PressA : boolean = false;
var ThePlayer : Transform;

var DoorClosed : boolean = true;
var DoorOpen : boolean = false;

function Start () 
{

}

function Update () 
{
	var distance : float = Vector3.Distance (ThePlayer.transform.position, transform.position);
	
	if (distance < 1.5f)
	{
		InRange = true;
		PressA = true;
	}
	else
	{
		InRange = false;
		PressA = false;
	}
}

function OnGUI ()
{
	if (InRange == true && PressA == true)
	{
		GUI.Box (new Rect (460, 495, 40, 25), "Open");
		
		if(InRange == true && Input.GetButtonDown ("Fire1") && DoorClosed == true)
		{
			animation.Play ("OpenDoor");
			DoorOpen = true;
			DoorClosed = false;
		}
		
		else if (InRange == true && Input.GetButtonDown ("Fire1") && DoorOpen == true)
		{
			animation.Play ("CloseDoor");
			DoorClosed = true;
			DoorOpen = false;
		}
	}
}

I have applied the two animations to the door and I have placed the player is the variable “ThePlayer”, but when I go to open it the door jumps open without the animation and then plays the closing door animation.
What am I doing wrong?

P.S Im using JavaScript

1 Answer

1

this

 if(InRange == true && Input.GetButtonDown ("Fire1") && DoorClosed == true)
       {
         animation.Play ("OpenDoor");
         DoorOpen = true;
         DoorClosed = false;
       }
 
       else if (InRange == true && Input.GetButtonDown ("Fire1") && DoorOpen == true)
       {
         animation.Play ("CloseDoor");
         DoorClosed = true;
         DoorOpen = false;
       }

needs to be Update() not OnGUI()

Thanks mate it works again. What a stupid mistake on my behave :)

No worries, we all make them from time to time.