使用方向矢量在一个方向上移动一个对象

时间:2022-02-12 22:58:18

Okay so I have a game that has object floating in the air. I want to simulate the wind by pushing the floating objects in the same direction of the wind.

好的,所以我有一个游戏,其中的物体漂浮在空中。我想通过沿着同一风向推动浮动物体来模拟风。

I have been looking on the internet but I have found no information on how to move an object by a (directional) vector instead of moving it towards a point in space.

我一直在网上看,但我没有找到关于如何通过(方向)向量移动对象而不是将其移向空间中的点的信息。

Vector.lerp, translate etc. all uses a point of reference where as, I would like to use a directional vector of length (ideally 1) from the center of the object towards it s wanted direction. This is possible with in-built forward vector but I wanted to do it even if the object is rotating on itself.

Vector.lerp,translate等都使用了一个参考点,其中,我想使用一个长度(理想情况下为1)的方向向量,从对象的中心朝向它想要的方向。使用内置前向矢量可以实现这一点,但即使对象在自身上旋转,我也希望这样做。

Anyone has an idea or a direction to go into?

任何人都有想法或方向进入?

PS: The directional vectors of all objects should be parallel. The only idea I have so far is to set a point by the wind vector and adjust it based for each objects. But that seems a bit complicated and bothersome to do for such a stupid task.

PS:所有对象的方向向量应该是平行的。到目前为止,我唯一的想法是通过风向量设置一个点,并根据每个对象进行调整。但这对于这样一个愚蠢的任务来说似乎有点复杂和麻烦。

Thank you!

2 个解决方案

#1


This is simply a matter of adding a vector to the object's position (or velocity if you want more realism)

这只是向对象的位置添加矢量(如果你想要更真实的话,还是速度)

transform.position += wind * Time.deltaTime

or

rigidbody.velocity += wind * Time.deltaTime

The second option requires the GameObject to have a Rigidbody component

第二个选项要求GameObject具有Rigidbody组件

#2


I think Transform has a member called Translate which handles the translation as a direction:

我认为Transform有一个名为Translate的成员,它将翻译作为一个方向来处理:

myobject.transform.Translate(2,0,0);

This moves the object 2 units to the right of where it is now.

这会将对象2个单位移动到现在的位置。

EDIT: here it is http://docs.unity3d.com/ScriptReference/Transform.Translate.html

编辑:这里是http://docs.unity3d.com/ScriptReference/Transform.Translate.html

#1


This is simply a matter of adding a vector to the object's position (or velocity if you want more realism)

这只是向对象的位置添加矢量(如果你想要更真实的话,还是速度)

transform.position += wind * Time.deltaTime

or

rigidbody.velocity += wind * Time.deltaTime

The second option requires the GameObject to have a Rigidbody component

第二个选项要求GameObject具有Rigidbody组件

#2


I think Transform has a member called Translate which handles the translation as a direction:

我认为Transform有一个名为Translate的成员,它将翻译作为一个方向来处理:

myobject.transform.Translate(2,0,0);

This moves the object 2 units to the right of where it is now.

这会将对象2个单位移动到现在的位置。

EDIT: here it is http://docs.unity3d.com/ScriptReference/Transform.Translate.html

编辑:这里是http://docs.unity3d.com/ScriptReference/Transform.Translate.html