how to open and close a door using raycast

Thankyou Berenger but still no luck. I checked whether the animation was working or not and I found that it was working. I now have changed the scrpit and attached it to the player.

This the new script :

var distanceOfPlayer = 10;

function Update () { var hit : RaycastHit; var toPlayAnotherAnimation : boolean = false; var b = gameObject.FindWithTag("Door");

    //check if the player is colliding.....
if(Physics.Raycast(transform.position, transform.forward, hit, distanceOfPlayer))
{
    //......with a door
    if(hit.collider.gameObject.tag == "Door" && toPlayAnotherAnimation == false)
    {
        //open the door
        hit.collider.gameObject.animation.Play("Door_Open");
        toPlayAnotherAnimation = true;
    }

    if(transform.position.z < -11 || transform.position.z > 11)
    {
        if(toPlayAnotherAnimation == true)
        b.animation.Play("Door_Close");
    }
}

}

The animation "Door_Open" is such that the door moves along the +y axis leaving just enough room for the player to pass through. So when the player reaches the viscinity of the door it animates and the door opens but when I move away the door does not close. I checked the animation "Door_Close" and its working. PLEASE Help!!!!!!!!

There is several steps you should do:

  • Make sure the animation works : do the 'animation.Play("Door_Open");' when you hit space bar for instance.
  • Make sure the player have the right tag, the "Player" one.
  • Don't use raycast in that case, use a box collider wider than the door instead. Then put the code that open the door in a OnColliderEnter function.

Hope that help.

I did it!!!!!!!!!!!! post the script in a couple of days :)