Hello,
I’m trying to make a Rigidbody player controller that has full interaction with all in-game physics.
Turns out this is incredibly complicated and difficult to do properly! All the existing examples out there have several things wrong with them that I’m having a hard time fixing.
The problem is that the only way to get satisfactory player movement using a Rigidbody is by using a target velocity, then applying forces to reach that velocity. Unfortunately, this means that as soon as any external world physics act upon the player (ie hits a spinning wheel which should launch him, or something bouncy, or even trying to maintain momentum off a moving platform), all momentum is lost, because the controller immediately applies a Force to counter-act the player’s current velocity, to bring it in line with the player’s input.
This is unrealistic and highly undesirable in my physics-based game.
I thought of a possible solution: create a “ghost” Game Object which is tied to the player’s input as usual. It moves around using the targetVelocity forces but is invisible.
Then, to move the player, simply add the current Vector3 velocity of that “ghost” object to the player’s current velocity.
Unfortunately, this will of course just results in the overall velocity stacking on top of itself forever. Even though the “ghost” object’s maximum velocity is limited, the player’s speed will be (current speed + ghost speed) - so the player will just keep speeding up.
I cannot limit the maximum speed of the player, because I want external forces to allow him to be pushed by something in the world very fast.
I want the external force to push him fast, then as its force “wears off” by friction, drag, gravity, etc, slowly come to the velocity of only the input (ghost object).
So I really just want a way of scripting this: “The player’s current velocity is equal to the player’s input velocity PLUS velocity incurred via external forces BUT NOT velocity incurred via previous player input acceleration.”
To do this, I need some way of interrupting the PhysX engine to check what the source of a force was, then only apply forces to objects once that has been checked and factored into the calculations.
How can I do this?