- Home /
Finding the normal/perpendicular
So i've seen a bunch of questions similar to this, but much more complicated and i've not been able to discern the answer, so i'm asking again.
I'm not very experienced so simpler is better:).
Basically i'm trying to make a tennis game, but for now i'm just trying to make a wall return the ball to me. I've managed to make it do that by hard coding the return vector:
void OnCollisionEnter(Collision collision) {
GameObject ball = collision.gameObject;
ball.GetComponent<Rigidbody>().velocity = new Vector3(0,8,-8);
}
What i'm trying to do now, is if I change the angle of the collider, the ball should bounce perpendicular to the wall. Because the vector is hard coded, regardless of where the ball is facing the ball bounces back the same.
I've tried using Vector3.cross to try to dynamically get the perpendicular vector (then I can add force to maniuplate how strongly it comes back), but my problem is, I dont now how to get the two sides of the walls as vectors.
You can use Vector3.Reflect. Note that the Collision class also includes an array of hits, each one of which has a normal variable that returns the normal vector3 of the hit triangle.
I think Cherno's suggestion will be the most practical way to get what you are looking for. But wanted to answer this question....
I dont now how to get the two sides of the walls as vectors.
I'm going to make a couple of assumptions: without rotation, the wall faces the screen directly: in other words- it is defined in model-space as lying on the XZ plane. In THIS default orientation, the vectors for 2 wall sides are(1,0,0) and (0,0,1).
Now that we have the two vectors, in model space, we need to orient them appropriately in worldspace.
wall.transform.roatation will give the orientation of the wall in worlspace.
We can multiply this rotation Quaternion by our wall-side-vector in modelspace, to get the wall-side-vector in worldspace.
Vector3 verticalSide= wall.transform.roatation * Vector3(1,0,0); //order matters in transform arithmatic)
Vector3 horizSide= wall.transform.roatation * Vector3(0,0,1);
As you noted, the cross product of these two vetors will be the normal of the wall. Note: you could also compute or note(0,1,0) the normal in model space, and rotate just that one vector.
Your answer
![](https://koobas.hobune.stream/wayback/20220612000054im_/https://answers.unity.com/themes/thub/images/avi.jpg)