简体   繁体   English

在 Unity 中更改默认对撞机反弹方向

[英]Change default collider bounce direction in Unity

I'm creating a Breakout clone in Unity and want to change the direction of the ball based on if the ball hits the left side or the right side of the paddle, as in the original version of the game.我正在 Unity 中创建一个Breakout 克隆,并希望根据球击中球拍的左侧或右侧来更改球的方向,就像在游戏的原始版本中一样。 What is the best way of accomplishing this?完成此任务的最佳方法是什么? I tried adding angled colliders to my paddle, but that causes the ball to fall slightly through the paddle before hitting the collider and changing direction.我尝试在我的球拍上添加成角度的碰撞器,但这会导致球在撞击碰撞器并改变方向之前稍微穿过球拍下落。

You should have access to the rigid body that you are colliding with, which will tell you the angle you want to bounce at.你应该可以访问你正在碰撞的刚体,它会告诉你你想要反弹的角度。 Then, you do a late modification of the trajectory ("late" as in after your physics are calculated and applied for velocity).然后,您对轨迹进行后期修改(“后期”是在计算物理并应用于速度之后)。 Something like this should work:这样的事情应该有效:

Rigidbody2D.velocity = newDirection.normalized * Rigidbody2D.velocity.magnitude;

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

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