- Home /
Normal of collision hit is moving downwards
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:
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;
}
}
}
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.
Check to make sure that your normal vector isn't (0, 0), because this can be returned when things are facing in parallel directions. Well if you're getting a normal vector of (0, 0), just test the function at other angles, and use an if statement for the case where they're parallel. That's what I did when I was trying to rotate something. i.e.
if (theNormal != Vector3.zero) Vector3 reflection = Vector3.Reflect(bulletDirection, theNormal);
else bulletDirection = -bulletDirection;
But I could be wrong.
Your answer
Follow this Question
Related Questions
Collision with no contact? 2 Answers
How do I find the point of contact on a box collider? 1 Answer
Detecting collision on start in my building system 0 Answers
Not detecting a simple collision? 2 Answers
Raycast from inside an object 2 Answers