- Home /
Prevent rigidbody being affected by character controller
There are potentially 3 ways I've thought of which I've put in bold, that if I figure out just one of them, then my stuff will work.
I'm trying to write a script so that my character can be crushed by objects. I'm using a CharacterController for what I'll call the 'controller' and the objects crushing him are rigidbodies which I'll call the 'crusher'.
The crusher is currently using a non-kinematic rigidbody, gravity disabled and constraints set to all. I can detect being crushed perfectly fine, the problem is when my controller, say jumps, it pushes the crusher away making it fly off. How can I stop a character controller from moving the rigidbody?
I've tried setting rigidbody.velocity = Vector3.zero; every FixedUpdate. This stops the crusher flying away but you can still notice resistance.
Now if I make crusher a kinematic, it works fine, but I lose the ability to use OnCollisionEnter() and therefore the way I'm detecting the player being crushed. Is there anyway to detect a collision between a kinematic and a character controller without using OnControllerColliderHit?
Would attaching a rigidbody to my controller give me back OnCollisionEnter?
Finally I thought about using a trigger on the crusher and using OnTriggerEnter, but if I jump I just pass through the crusher. Is there anyway to prevent a collider passing through a trigger? I tried attaching a trigger to another rigidbody as a child but I still passed through it most of the time. Would separating them and position them at same place through a script work?
Cheers for advice.
-edit-
Got a video of current issue.
http://bovinelabs.com/crush/crush/
This is using a rigidbody kinematic and a separate trigger
The crusher moves on FixedUpdate() using
rigidbody.MovePosition(rigidbody.position + _direction*Speed*Time.deltaTime);
CharacterController is a heavily modified version of the FPSwalker to allow stuff like wall jumping, but I do not believe any changes would affect this.
Notice it only really does it when it's going down, and I'm moving up
Answer by Brian-Kehrer · Feb 21, 2011 at 12:18 AM
Use a trigger to detect crush, and then have a kinematic rigidbody collider to prevent your character controller passing through your object. In short, two separate objects (preferably set to ignore each other).
If you make the trigger a bit smaller than the collider, you can have some 'safety' region, so you can't jump into the crush trigger.
O$$anonymous$$ tried this again. I still get same issue I had last time I did that. About 30% of the time I still jump through the platform. Only way I can seem to fix this is if I crank up skin width to around 50% of the radius, but this causes hovering. Also OnTriggerEnter doesn't fire off unless my controller is also moving (falls asleep?) I'll record a video of what's happening.
Answer by Joshua · Feb 21, 2011 at 01:31 AM
To stop your character from pushing the crushers away you could always make the weight of them huge compared to the characters str.
Weight on 100,000. Doesn't seem to change anything when you use a constant move on a rigidbody from a character controller.
That is really strange. But, do you want your character to keep moving after he is crushed? You could make the crusher a trigger and implement a line in your charactermovement script that disables it when crushed.