简体   繁体   English

Unity/C#:在垂直跳跃前检查速度?

[英]Unity/C#: check velocity before vertical jump?

How do you convert rb.velocity into an integer?你如何将 rb.velocity 转换为整数?

I attempted to have if (rb.velocity < 0) to then make a jump be able to add to existing momentum without jumps doing nothing if the negative momentum is to high.我试图让if (rb.velocity < 0)然后进行跳跃,如果负动量太高,则跳跃能够增加现有动量而不会跳跃。

To make the comparison, you just need to do if (rb.velocity.y < 0) .要进行比较,您只需要执行if (rb.velocity.y < 0) This checks if the vertical velocity is less than 0, which is what you really want.这将检查垂直速度是否小于 0,这是您真正想要的。

However, I would also recommend you instead set rb.velocity = 0 every time you jump before applying the jump force (or however you may be making the jump) for consistency.但是,我也建议您在每次跳跃之前设置rb.velocity = 0然后再施加跳跃力(或者您可能正在跳跃)以保持一致性。

In order to get the velocity to an int it's pretty easy为了使速度达到 int 很容易

int speed;
//By grapping the total magnitude which is x*x+y*y+z*z we get total speed value
speed = (int)rb.velocity.magnitude;

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

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