- Home /
Door Not Colliding
I've successfully created a working door. It opens and closes when clicked and collides with the player fine. But when the player is running into the door right before the door is clicked, he goes right through.
This script is attached to the door, which is a transform child to an empty "hinge" object, which is being rotated.
public bool Opened = false;
bool bounced = false;
public float SwingSpd = .01f;
public float MaxAngle = .87f;
public bool Swingin = false;
//temp crap
Quaternion CrappyDoo;
void Start()
{
rigidbody.constraints = RigidbodyConstraints.FreezeAll;
}
//making sure door goes back if it hits a collider. (DUN DUN DUN)//
void OnCollisionEnter(Collision NotUsingThis)
{
if (!bounced && Swingin)
{
SwingSpd /= 2;
if (Opened)
{
Opened = false;
}
else
{
Opened = true;
}
bounced = true;
}
}
void Update()
{
if (/*mouse click*/)
{
//toggle "Opened"
Swingin = true;
}
CrappyDoo = transform.parent.localRotation;
if (transform.parent.localRotation.y <= MaxAngle)
{
if (Opened && Swingin)
{
CrappyDoo.y += SwingSpd;
transform.parent.localRotation = CrappyDoo;
}
}
else if (Opened && Swingin)
{
Swingin = false;
if (bounced)
SwingSpd *= 2;
bounced = false;
}
if (transform.parent.localRotation.y >= 0)
{
if (!Opened && Swingin)
{
CrappyDoo.y -= SwingSpd;
transform.parent.localRotation = CrappyDoo;
}
}
else if (!Opened && Swingin)
{
Swingin = false;
if (bounced)
SwingSpd *= 2;
bounced = false;
}
}
I have no clue what's wrong with the code. The kinematic box is unchecked on the door's rigidbidy (otherwise it doesn't collide at all). I can't figure why it would ever not collide.
Ins$$anonymous$$d of showing you a way of fixing what you have, I will simply give you an idea on how to write a code for your door-job that cannot fail at this task. Ins$$anonymous$$d of using rigidbodies, just use colliders for doors and move the doors using localEulerAngels, which will give you the ability to directly manipulate each rotation axis in 360° circles ins$$anonymous$$d of the more complex quaternions.
//example
public GameObject Flag;
public ButtonControllerScript $$anonymous$$yButton;
void Update()
{
if($$anonymous$$yButton.isClicked)
Flag.transform.localEulerAngles += new Vector3(0f,30f, 0f);
}
Also, make sure that whatever shouldn't penetrate the collider is moving using physics correct movement, like rigidbody.AddForce() - but such a thing is self explenatory since otherwise all characters controlled by players could just move through walls and terrain.
I think you're still moving the collider by moving it's root, because they share hierachy. A forced move (or roatate) will negate collisions. It may work better if your door is not a child of the "hinge". Do you need an angular costraint with a target rotation that you set for open and closed? freeze only the position and x and z rotations, you'll need y free.