- Home /
My character controller wont detect collision when crouching.
Hi so I have arrows as triggers flying at my character. If hes standing the onTriggerEnter function works just fine, he loses one health and is knocked back. However, if I crouch nothing I dont get hit, for some reason the OnTriggerEnter function isn't being called. I turn off the mesh renderer and look at my collider on the scene view and even though the arrows are clearly entering the player the onTriggerEnter function doesn't detect it.
function Crouch()
{
if (controller.isGrounded && Input.GetKey("s"))
{
crouching = true;
controller.height = crouchHeight;
controller.center.y = crouchCenterY;
}
else if (Input.GetKeyUp("s"))
{
gravity = 0;
crouching = false;
controller.height = standHeight;
controller.center.y = standCenterY;
gravity = normalGravity;
}
}
function OnTriggerEnter (other : Collider)
{
if (!invincible)
{
if(other.gameObject.tag == "enemy")
{
print("gotHit");
hit = true;
health -= 1;
invincible = true;
yield WaitForSeconds (2.5);
invincible = false;
}
}
}
$$anonymous$$ake sure your character controller collision box is going where it needs to go. Check the green outline in the inspector and see where the collider is going.
I turn of the mesh renderer for the capsule and in the scene view the capsule collider is what I'm looking at. I can't figure out what going on.
adjust the levels of height. With just code I don't know what is going on at all. I don't even know what your problem is. You are just simply saying. It doesn't work.
so I changed controller.center.y to transform.position.y and now its working. However, now I have a new problem. If I press the crouch button crazy fast my character falls through the floor.
It is tricky. If the collider is already colliding with some some object at that start it goes crazy.
Ex. You put a collider on an object so it wont fall through the plane. Put the collider just .001 units into the plane and you immediately fall through. So, this just requires careful tinkering.
Answer by MrRandomFella · Apr 08, 2014 at 09:32 AM
I'm not too sure about this, but anyway. I have tried to use a similar crouching script to yours and I've noticed that collisions with the floor seem to disappear whenever I crouch too low. So remember to set that at a reasonable height. Secondly, have you tried using the "DontGoThroughThings" script on the wiki? I think your controller may be failing to detect the fast moving arrows whenever you crouch. This is just a thought of mine, I don't know if this will fix your problem. Script
tried :/
thanks for the answer. This is an old question, but currently I am using hitboxes ins$$anonymous$$d. When I hit down to crouch my standing hitbox will deactivate and the crouching one will activate. Of course this is for an entirely different project but I suppose it would work for this one too. I'll have to go back and try it.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
OnTriggerEnter - destroy (this.gameobject) if it collides with anything 2 Answers
Collider doesn't transfer to function 1 Answer
Null reference that isn't null 2 Answers
Error accessing var from other scripts within project via GetComponent 1 Answer