简体   繁体   English

如何计算旋转单位3D C#的速度

[英]How can I calculate the speed of a rotation unity 3d c#

so the thing is: i have a sensor that makes a rigidbody rotate, the problem is that i can't calculate the speed in which it was rotated (If the sensor was moved fast the object would also move fast but when it collides with another gameobject it won't apply the speed/force which with it has been rotated) Is there any way I can calculate the speed/force in which it has recently rotated so that i can apply that speed/force to another object when it collides? 所以问题是:我有一个使刚体旋转的传感器,问题是我无法计算其旋转的速度(如果传感器快速移动,则对象也会快速移动,但是当它与另一个碰撞时它不会应用已旋转的速度/力的游戏对象)有什么方法可以计算它最近旋转的速度/力,以便在碰撞时可以将速度/力应用于另一个对象?

note: im not entirely sure if what i need is speed or force 注意:我不确定我需要的是速度还是力量

float rot = float.Parse (sp.ReadLine());
transform.eulerAngles = new Vector3 (0, 90, rot); 

note: because of how the object was designed it has to be rotated by default in 90 degrees. 注意:由于对象的设计方式,默认情况下必须将其旋转90度。 Basically what im doing is receiving information from a Gyroscope and puting it in the object rotation. 基本上,我正在做的是从陀螺仪接收信息并将其置于对象旋转中。

The sensor gives values from -90 to 90 in the form of Float, when it is rotated to the left it gives values from -90 to 0, when rotated to the right it gives values from 0 to 90. this values should be used to graphically rotate the object and also aply the speed in which it was rotated when it collides whith another gameobject (if it was moved fast from -90 to -20 for example it should apply more force than if it was moved slowly from -90 to -50 to -20) 传感器以Float的形式给出-90至90的值,向左旋转则给出-90至0的值,向右旋转则给出0至90的值。该值应用于图形化地旋转对象,并使其与另一个游戏对象碰撞时的旋转速度也适当(例如,如果将其从-90快速移动到-20,则它所施加的力应大于从-90缓慢移动到- 50至-20)

When trying to cause believable physical interaction between objects, you never want to manipulate the transform directly. 当试图引起对象之间可信的物理交互时,您永远不想直接操纵transform Instead, you should be making use of the various methods available to physically influence an attached Rigidbody . 相反,您应该利用各种可用的方法来物理影响附着的Rigidbody

To apply rotation in this case, you're going to want to use Rigidbody.AddTorque() . 在这种情况下,要应用旋转,您将要使用Rigidbody.AddTorque() Here's an idea of how you might use it: 这是一个有关如何使用它的想法:

// Probably get this value in Start() and save it for later
Rigidbody rb = GetComponent<Rigidbody>();

float rot = float.Parse (sp.ReadLine());

// transform.forward is the unit vector corresponding to the local z-axis of the object
Vector3 rotationAxis = transform.forward * rot;
rb.AddTorque(rotationAxis);

Hope this helps! 希望这可以帮助! Let me know if you have any questions. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM