Unity - how to use Vector2.Reflect()

前端 未结 2 847
野性不改
野性不改 2021-01-18 18:27

I have looked everywhere including the Unity documentation but cannot seem to find any good examples of how to use Unity\'s Vector2.Reflect() function. I am trying to use th

2条回答
  •  终归单人心
    2021-01-18 18:52

    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().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.

提交回复
热议问题