简体   繁体   English

在 Unity 中如何解决 2D 碰撞中的弹跳问题?

[英]How do you fix bouncing in 2D collision in Unity?

My game is a topdown 2D game, and when the player collides with another object while it is moving the player starts bouncing off.我的游戏是一个自上而下的 2D 游戏,当玩家在移动时与另一个 object 发生碰撞时,玩家开始弹跳。 I've tried using a 2D physics material with 0 bounciness and it doesn't solve the problem.我尝试使用弹力为 0 的 2D 物理材料,但并没有解决问题。 I think it has something to do with the way my movement code.我认为这与我的运动代码方式有关。 I tried using Rigidbody.MovePosition, but it didn't work and my player was glitching out.我尝试使用 Rigidbody.MovePosition,但它不起作用,我的播放器出现故障。 This is my movement code.这是我的运动代码。

float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
float vertical = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;

transform.Translate(new Vector2(horizontal, vertical));

Try using the MovePosition method on your Rigidbody inside of FixedUpdate instead of changing the position of the player's transform.尝试在 FixedUpdate 内部的刚体上使用MovePosition方法,而不是更改玩家变换的 position。 If that does not solve the issue, try also changing the collision detection mode of the player's rigidbody to continuous.如果这不能解决问题,请尝试将玩家刚体的碰撞检测模式更改为连续。

When working with physics and Rigidbodies in Unity, you would want to do most of the physics related things in the FixedUpdate method.在 Unity 中使用物理和刚体时,您可能希望在 FixedUpdate 方法中完成大部分与物理相关的事情。 This method runs in sync with Unity's physics loop and all the physics calculations are done right after all of the FixedUpdate methods have been called.此方法与 Unity 的物理循环同步运行,并且所有物理计算都在所有 FixedUpdate 方法被调用后立即完成。 This allows for smooth looking interactions and physics.这允许看起来平滑的交互和物理。

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

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