- Home /
How can I push a Character Controller to the closest edge of a physics object?
Hey guys,
The problem I am having is that CharacterController's collision physics fail when dealing with a moving physics object.
I have a CharacterController, and I would like to simulate it being pushed by a moving physics object, which in this case is a cube with a Rigidbody. Unity does not make this very easy, since OnControllerColliderHit() only works if the CharacterController is moving.
I have attempted to add a Trigger to the cube, which, when the CharacterController enters the Trigger, a script will make the CharacterController move the same amount that the cube does. This works well enough as long as the CharacterController and cube are not moving towards each other. When they move towards each other, the CharacterController's physics fail, and the two objects pass through each other.
I have tried countless things, such as using a Rigidbody and CapsuleCollider instead of a CharacterController, but this causes way too many problems with game play to be a viable solution.
My current idea is to try to get the CharacterController to push itself outside of the cube whenever it collides with it, but I am having trouble figuring out the best way to use the hit information to do this, especially when other forces may be pushing the CharacterController towards the cube at the same time.
How might I, taking a movement Vector into consideration, use the hit info given in OnControllerColliderHit() to push the CharacterController outside of any cubes it may collide with? Any ideas or advice welcome.