For example, you might use something like:
public Vector3 Inertia = new Vector3();
void Update()
{
Inertia += transform.TransformDirection(Vector3.forward) * Input.GetAxis("Thrust") ;
transform.Translate(Inertia * Time.deltaTime);
}
If you want fly a spaceship in a 3D space with all degrees of freedom.
Inertia keeps track of momentum in world space while the controls add to it along the axes of the players orientation.
But, now I want to check what the players speed is along his current “forward” axes. How do you extract a value out of a vector3 along a arbitrary axes?
So you want the players velocity along the local axis? You can simply use transform.InverseTransformDirection(velocity)
which gives you a local space vector where the z component is the amount of speed along the local forward axis… Likewise you also get the amount along the other local axes.
You should rename your variable “Inertia” to “velocity” as inertia is kind of the opposite what you have in mind. Inertia is the “amount of resistance to change in velocity” (to quote wikipedia). The “linear momentum” of an object is its velocity multiplied by its mass. A rigidbody simulation ususually keeps track of the objects position and rotation as well as the linear and angular velocity.
Finally instead of using transform.TransformDirection(Vector3.forward)
you can simply use transform.forward
which gives you the same thing.
Not exactly sure, if i understand it correctly. Like @Bunny83 wrote, best way in this case is usually using the transform.forward
vector.
But if you need only the part of that vector along an arbitrary axes, you can try to “project” this one onto another Vector (e.g. in Worldspace) by using Vector3.Project()
.