如何获取Unity汽车游戏中的车速
我开始了汽车游戏并开发了AI、汽车控制脚本。
现在我想知道汽车的速度。
我使用了下面的代码。
BackLeftWheel.wheelTorque = EngineTorque / GearRatio * 4;
汽车行驶得很顺利,但我想知道它的确切速度。
I have started the car game and developed AI, car control script.
Now I want to know the speed of the car.
I have used the below code.
BackLeftWheel.wheelTorque = EngineTorque / GearRatio * 4;
The car is going smoothly, but I want to know its exact speed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果汽车有刚体,您只需执行rigidbody.velocity.magnitude即可获得速度。 请参阅此处
但有一些注意事项:不要在 Update 中调用 GetComponent,而应在启动并存储结果。
另外:计算幅度需要平方根运算。如果可能的话,应该在更新中避免这种情况(尽管,项目中有一些可能不会太重要)
If the car has a rigidbody, you can just do rigidbody.velocity.magnitude to get the speed. See here
some notes though: don't call GetComponent in Update, call it in Start and store the result.
also: calculating magnitude requires a square root operation. if possible this should be avoided in Update (although, having a few in the project probably won't matter too much)
我不熟悉你正在使用的汽车,但是只要你的脚本可以访问你的汽车的变换,你就可以随时根据时间检查最后的位置,并以这种方式更新速度......
I am not familiar with the car you are using, but as long as your script has access to the transform of your car, you can always check the last position based on time, and update the speed this way...