R
R
RandomProgrammer2022-01-05 16:07:59
Unity
RandomProgrammer, 2022-01-05 16:07:59

How to calculate the motion vector?

I am currently making my PlayerController based on a Rigidbody. I move on an inclined surface, and depending on the slope I move not only in x and z, but also in y:
61d5968dd3094394677069.png
Here is my code:

float groundAngle = (float)System.Math.Acos(Mathf.Clamp(groundNormal.y, -1, 1)) * 180 / (float)System.Math.PI;
rigidbody.AddRelativeForce((direction + Vector3.up * groundAngle / 45) * speed, mode);

But this method is only suitable if the player is moving straight up the hill. But it can also go down or go along an inclined surface. In general, I want to calculate the motion vector, knowing where the player will go and the normal of the surface.
PS direction - the player's local direction.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2022-01-05
@RandomProgrammer

If you have a PlayerController with a collider + Rigidbody and a collider on a supporting surface, it's enough to apply force just in the direction of view. (transform.forward) Uphill or downhill movement will be decided by the physics engine itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question