简体   繁体   English

Unity:碰撞后改变 RigidBody2D 的速度会导致游戏崩溃,无论平台如何,包括在编辑器中

[英]Unity: Changing velocity of RigidBody2D after collision causes game to crash regardless of platform, including in the editor

This line of code is single handedly causing my game to crash only sometimes.这行代码单枪匹马地导致我的游戏有时会崩溃。 It essentially freezes the editor and I have to force quit Unity, and same on mobile.它基本上冻结了编辑器,我必须强制退出 Unity,在移动设备上也是如此。

_rb.velocity = new Vector2(_rb.velocity.x, -20f);

I've tried setting a flag in the collision and updating velocity on the next fixed update.我尝试在碰撞中设置一个标志并在下一次固定更新时更新速度。 I've tried updating the velocity directly in the EnterCollision2D method.我试过直接在 EnterCollision2D 方法中更新速度。

Notably I haven't been able to recreate the crash when applying force instead of altering velocity, but applying force doesn't give the desired effect.值得注意的是,当施加力而不是改变速度时,我无法重现崩溃,但施加力并没有产生预期的效果。

The editor logs don't have anything in them either, so I'm pretty confused.编辑器日志中也没有任何内容,所以我很困惑。 Any help would be greatly appreciated!任何帮助将不胜感激!

Im pretty sure the collision isnt the poroblem here.我很确定碰撞不是这里的问题。 Make sure you don't have any infinite loops in your code (calling the same method inside of the method or using while loops outside of coroutines).确保您的代码中没有任何无限循环(在方法内部调用相同的方法或在协程外部使用 while 循环)。 Also if you are using unity 2018 it tends to crash quite fast.此外,如果您使用的是 unity 2018,它往往会很快崩溃。

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

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