- Home /
Best Strategy to interact with rigidbody
I am experimenting with building a game which has two main requirements:
A playable character which moves as with a character controller (quick acceleration and sudden stops, quick turns, no gravity effect, etc.)
The ability to interact with object using realistic physics that make the most sense with a rigid body (for example, my character runs into a box and pushes it off of a shelf onto an enemy below).
So my question is: what is the best strategy for building a game with the best of both worlds? If I want to use a character controller on my character, and a rigid body on my box, how can I make a man hit the box and knock it off? It appears that the rigid body and CC just don't work together - so what combination DOES work?
And also, I'm aware that the simple task of pushing a box off of a ledge would be fairly easy if I coded it by hand, but I'm looking for realistic physics and also interaction with a wide variety of environmental items, so coming up with a way to hard-code transforms for everything in the game without physics just won't cut it.
Answer by JustinTheSwift · Dec 16, 2018 at 08:00 PM
I found the solution to my own problem.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnControllerColliderHit.html
That page shows how to use the OnControllerCollider method of the CharacterController. The method detects a hit against a rigid body and defines the direction in which the box ought to be pushed.
Vega's answer (while appreciated) is not correct.
Because, if I understand you correctly, you're saying that my character controller should automatically interact with a rigid body, but it doesn't. You seemed to say that adding a rigid body to a box would solve the problem, but my question explicitly stated that I had already done so to no avail. I had to implement the method mentioned above to describe the interaction between the controller and the rigid body to make the box move.
If you are moving the character controller via move, then you are correct. I would recommend moving it via transform.position. Having to deal with your own forces is absurd for what you are wanting to do. (I made the assumption you were moving it with transform.position - which will indeed move other objects).
Your answer
Follow this Question
Related Questions
My player is stuck on left and right side(Endless Runner) 0 Answers
How does the premade FPSController handle character physics? 1 Answer
Physics goes crazy when rigidbody collides with moving character controller 0 Answers
Making a Character Controller push another Character Controller 3 Answers
FPS Character Controller with the push DOWN rigidbodies ability 0 Answers