I have a tank that is supposed to bounce with Unity´s reflect function. What I do, is that I check the collision contact, throw a raycast to that position and get the normal of the collider I hit, so that I can use Unity´s reflect option...
But for some reason, the normal and the reflection is moving downwards...
Green line is the normal, and the red one is the reflect.
Here is the code:
![alt text][1]
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("bounceable"))
{
if (hitByABullet)
{
Vector3 theNormal = other.GetContact(0).normal;
Vector3 hitPos = other.GetContact(0).point;
RaycastHit theHit;
//if we hit the collider with a raycast...
if (other.collider.Raycast(new Ray(transform.position, hitPos), out theHit, 3))
{
//get the NORMAL of the collider we are hitting
theNormal = theHit.normal;
}
//bulletDirection is actually the transform.forward of the bullet that was shot by the other tank
Vector3 reflection = Vector3.Reflect(bulletDirection, theNormal);
//draw the normal of where the raycast hit...
Debug.DrawLine(hitPos, theNormal, Color.green, 10f);
//draw the reflection
Debug.DrawLine(hitPos, reflection * 5, Color.red, 10f);
//push the tank that way...
currentRb.AddForce(reflection * 5f, ForceMode.Impulse);
hitByABullet = false;
}
}
}
[1]: /storage/temp/133534-atanks.png
By the way, what exactly does GetContact store? What I understood from the documentation is that it stores the actual collision point where the collider was hit, but that may not be the case. There is really poor documentation regarding that topic.
↧