- Home /
Player can't block door
I have a door controller script as outlined below. The door opens and closes when activated by the player.
Now I'm trying to implement code that sets the door to "blocked" if it detects via raycast that the player stands under it (it's an upwards sliding door).
The Raycast detects the player and even prints "door blocked" as it should, but it won't change the "blocked" value to "true".
I have no idea why it won't change, my own idea was that it can't be changed in the Update function so I tried calling seperate block and unblock functions inside the Update instead but it wouldn't work either :(
var blocked : boolean = false;
function Update ()
{
var hitCollision : RaycastHit;
var DoorRaycastOffset = transform.position + transform.up * 0.3;
if (Physics.Raycast (DoorRaycastOffset, (-transform.up), hitCollision)) //check if there's a collider below the door (when it's open = has moved up)
{
if(hitCollision.collider.gameObject.CompareTag("Player")) //is the collider a Player?
{
Debug.DrawRay(DoorRaycastOffset, (-transform.up), Color.green);
blocked = true; //--> this doesn't work!
print("door blocked");
}
else
{
blocked = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
check bool in WaitForSeconds 2 Answers
Raycast is being unreliable 1 Answer
Boolean doesn't change at start position. 1 Answer