Unity - how to use Vector2.Reflect()

和自甴很熟 提交于 2019-12-02 02:47:44

Vector2 Reflect(Vector2 inDirection, Vector2 inNormal):

inDirection: black arrow

inNormal: red arrow

return output: green arrow

The inDirection should be the velocity of your ball and the inNormal should be the unit vector that is perpendicular to your wall.

Try putting this in your ball object:

void OnCollisionEnter(Collision collision)
{
    Vector2D inDirection = GetComponent<RigidBody2D>().velocity;
    Vector2D inNormal = collision.contacts[0].normal;
    Vector2D newVelocity = Vector2D.Reflect(inDirection, inNormal);
}

NOTE: I cannot currently test that code, so it may need tweaking in terms of the names of things.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!