Blocking a door with an object

Hey everyone…
I’m trying to figure out how to get an object to block a door. Nothing I try seems to work consistently.

In the pics I attached you can see i just have a block that in the first pic the doors just completely pass through. The second one they manage to stop. It seems the only way the door stops is if the object is moving when the doors closed. If the block is stationary it’s like it doesn’t see them.

So I have two identical scripts on each door right now:

	void OnCollisionStay(Collision collision) {
		Rigidbody rigidbody;
		
		if((rigidbody = collision.gameObject.rigidbody) != null) {
			//Debug.Log (collision);
			hasCollision = true;
		}
	}
	void OnCollisionExit(Collision collision) {
		hasCollision = false;
	}

and then on the parent object of the door I have this:

if(leftDoorBlockScript.isColliding() == false  rightDoorBlockScript.isColliding() == false) {
  float percent = (((Time.time - startTime) * speed) / openDistance);
  if(percent > 1) animating = false;
  leftDoor.transform.localPosition = Vector3.Lerp (leftDoorOpenPosition, leftDoorClosePosition, percent);
  rightDoor.transform.localPosition = Vector3.Lerp (rightDoorOpenPosition, rightDoorClosePosition, percent);

When it does work, it works exactly how I want it to… it stops when a rigidbody is obstructing the door frame and when the object is moved it continues to close until it reaches its “closed” point. The way it’s acting though is that it doesn’t even see the collision. I tried slowing the doors down to see if it was the speed they were closing at and that didn’t seem to affect anything.

Any ideas?


Your rigidbody is probably falling asleep when its standing still making the collisions funky. Try keeping it awake and see if it is better and work from there.

My first thought is it could be colliding and leaving collisions with things other than the box which is messing it up. In your OnCollisionStay you’re testing if it’s got a rigidbody but you don’t test in your OnCollisionExit so if anything else leaves the collision with the door it will start moving again.

My second thought is if a Rigidbody collides with something it’s going to be pushed back until it’s out of the collision. Which in this case will trigger OnCollisionExit and start the door moving again, which will again collide with the Rigidbody and push it back.

Are you ensuring the doors don’t collide with the walls and floor through layers or something?

One approach I might try is using raycasts on the front edge of the doors, so you can stop before really colliding with the object, instead of stopping after colliding.

It would appear that the rigidbody was sleeping. I added kinematic rigidbodies to each door as they’re supposed to wake up sleeping rigid bodies and it is now working better.

As for your point rrh, I am definitely going to mess around with that element too… my logic was even if onCollisionExit set hasCollision to false because of some other object, the next frame it would be set back to true again. I know that’s a really messy and unreliable way of doing things but it didn’t seem to be the cause of this particular problem(though likely another problem down the road would result from that shoddy code :wink: