- Home /
How to make rigidbody addforce locally
I know that rigidbody.addrelativeforce is supposed to be what I'm looking for, but when I use it, it still does not work, but instead, sends my character going flying.
I am making an 8 direction character controller that rotates the character in the direction that it is walking. It rotates the character based on the keyboard inputs and camera rotation that works perfectly but my character walks in the same directions.
Here is a gif of what is happening
Here is the section that I use to move my character: Vector3 velocity = rigidBody.velocity;
Vector3 movementForce = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical"));
rigidBody.AddForce((movementForce * speed) - velocity, ForceMode.VelocityChange);
// Rotate character only if they are moving
if (rigidBody.velocity != Vector3.zero)
{
// Rotate character to the direction that it is moving + the rotation of the camera
float angle = Mathf.Atan2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * Mathf.Rad2Deg;
angle += mainCamera.transform.localEulerAngles.y;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.Euler(0, angle, 0), 0.15f);
}
From the looks of the gif I would say using Forces is not the right approach here. Just altering the velocity will give you a much better result in terms of character control. At least if you want to be able to immediately change direction from and to any direction.
Second, in the code you're actually using AddForce, not AddRelativeForce,, is that the correction already?.
Third, subtracting the current velocity results in incorrect behaviour because the velocity will accumulate on the rigidbody and therefore reach a point where its greater than the force you're trying to pass in, meaning you're always navigating in an opposite kind of way.
And finally, but I'm not sure about this one, rotating the rigidbody (teleport) you just applied forces to might also rotate the force in the new direction, but I'm not sure if that's so AND if that's intended.
I think you are forgetting momentum. You are adding the force as you intend. What you are not doing is adding an opposing force to stop drift. Essentially Pikachu is driftin' bra. $$anonymous$$ad skilz.
So if you want to stop that behavior while using physics add an equal & opposite force to the drift.
float velocity = Vector3.Dot(rigidbody.velocity, transform.right);
float force = rigidbody.mass * velocity / Time.fixedDeltaTime; // Time.fixedDeltaTime assumes using FixedUpdate
rigidbody.AddForce(transform.right * -1f * force);
@x$$anonymous$$riostar Yet another edit :D
Answer by IgorAherne · Sep 23, 2017 at 09:25 AM
you can express the forward vector (or any other direction local to the character) of a character like this:
Vector3 forceRelativeToWorld = myCharacter.transform.localToWorldMatrix.MultiplyVector(Vector3.forward);
You can then use this vector as a directon for your rigidbody force
That doesn't seem to help me solve my issue. At least, I don't know how to implement it to solve my issue. $$anonymous$$y character rotates in the direction that it moves, the problem I am having with my code above is that the character moves in the same direction no matter which way I am rotated