Unity:如何找到沿特定轴的向量 3 的值? [英] Unity:How to find the value of a vector 3 allong a specific axis?

查看:35
本文介绍了Unity:如何找到沿特定轴的向量 3 的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,您可以使用以下内容:

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);
 }

如果您想在具有所有自由度的 3D 空间中驾驶宇宙飞船.Inertia 跟踪世界空间中的动量,而控件则沿玩家方向的轴添加.

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.

但是,现在我想检查玩家沿着他当前前进"轴的速度.如何沿任意轴从 vector3 中提取值?

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?

推荐答案

如果你有一个任意轴(称之为 axis)和一个任意向量(称之为 vector>) 并且你想在 axis 的方向上找到 vector 的向量分量,你可以只用点积来做到这一点.(请参阅矢量投影).

If you have an arbitrary axis (call it axis), and an arbitrary vector (call it vector) and you want to find the vector component of vector in the direction of axis, you can do that with just the dot product. (see vector projection).

标量投影
vectoraxis方向的分量为:

float component = Vector3.Dot(vector, axis.normalized);

例如:如果轴是 z 轴,这将是向量的 z 分量.
component 的绝对值是投影向量的大小.

Eg: If the axis was the z axis, this would be the z component of the vector.
The absolute value of component is the magnitude of the projected vector.

矢量投影
vectoraxis 上的投影为:

Vector3 proj = axis.normalized * component;

这篇关于Unity:如何找到沿特定轴的向量 3 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆