- Home /
Vector direction after collision
I'm trying to make a "hook" with some given direction and length.
Until that, okay, i get the direction from my mouse click position:
Vector3 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition);
And I need to move the "hook" head from my position to the "end" position:
Vector3 end = direction * length;
Now, i need to add some functionality to it, which is, when the "hook" head collides with a wall (and it still has some length available), i get the resulting direction of that collision (that's what i need), multiply it to the remaining length and i have my updated end point.
I tried to do that with the Vector3.Cross(..), passing as parameters the Vector3.up and the hook current direction.
I'm not getting the result that i need, here are some examples of the direction i want:
Thanks in advance.
I found an article about that, actually i need to do some math with the "dot" function and the "wall" normal vector..
This page should give a good description of how to get your angle as well.
http://answers.unity3d.com/questions/317648/angle-between-two-vectors.html
Answer by Bunny83 · Dec 10, 2014 at 08:26 AM
Actually all you need to calculate the reflected vector is the surface normal vector (which you get from a raycast) and the Vector3.Reflect method.
ps: ScreenToWorldPoint takes the distance from the camera as z component. If you pass a Vector2 / mouse position the z value will default to 0. If you use a perspective camera that means it will always return the camera's position since at 0 distance all points meet at the same point. See the example on the documentation page. They use the cameras' near plane value. Also keep in mind that you will get a position, not a direction.
If you actually plan to do a raycast along that direction you might want to use ScreenPointToRay instead. It returns a ray that has it's origin at the camera position and a direction that points where you had your mouse position.