0
\$\begingroup\$

I am developing a first-person player movement in which the player should "fly" in the direction of his local forward vector.

This is working perfectly fine, however, when I turn the player to face other direction (rotate the y rotation axis), the force that has been added till this point, keeps significantly affecting the rigidbody so it moves the object towards the wrong direction, and it takes a lot of résistance force to move towards the "new" direction I am facing.

I'd optimally love to have a bit of "drifting" affect, so it maintains a realistic touch. I'm trying to achieve a behavior that is pretty much identical to a car behavior, so when the object is turning towards a direction, the object will actually almost instantly move towards this direction, maintaining its current velocity.

To make it clearer, here's an illustration of the current behavior: current behavior

Here's an illustration of the desired behavior: desired behavior

This is the current code I'm using:

        //Add force up 
    playerRB.AddForce(Vector3.up *  wingForceUpMultiplier * forceUserInput, ForceMode.VelocityChange);
       //Add force forward 
    playerRB.AddForce(forwardDirection * wingForceForwardMultiplier * forceUserInput, ForceMode.VelocityChange);

I am not necessarily looking for a complex-physics solution, it can be a simple workaround if there is any.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

If you want the velocity to rotate with the character it is easiest to just set the velocity manually, rather than control it with forces. The "best way" to code that is probably goind to depend on how you are rotating the character itself (for example, if you are using a Quaternion, you can just multiply the rigidbody.velocity by the same Quaternion.

A simple solution might be to do something like this right before the step where you add forces:

playerRB.velocity = forwardDirection * playerRB.velocity.magnitude;

But a more interesting one would be to handle this where the character is rotated:

void rotateCharacter(){
    Vector3 previousVelocity = playerRB.velocity;
    //now whatever code you use to rotate the character...
    playerRB.velocity = (forwardDirection*(1-driftAmount) + previousVelocity.normalized*driftAmount) * previousVelocity.magnitude;
}

Where driftAmount would be a value between 0 and 1, that controls how much inertia your character retains when turning (0 is no inertia, 1 is the character keeps moving as if it hadn't turned).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .