- Home /
raycast and trigonometry
Hi ! I'm trying to write from scratch, a simple pathfinding/object avoidance script.
my question is, how do I calculate a Vector3 coordinate, from a given origin Vector3 (my object position) and a raycast (that doeasn't hit anything)
While of course is easy to get the raycast hit coordinates for the right ray (the one hitting the box) what i want to know, is the vector3 coordinates, of the red point, given the "observer" transform, and the left raycast direction, and a choosen max length (in this cas the red dot)
thank you for your time :)
Answer by CHPedersen · Nov 08, 2011 at 05:21 PM
You can calculate that yourself using the parametric formula for a line in space. :)
Refer to this article on parametric equations, and notice in particular the Line example. That is in fact how ray casting works, and it is the reason you need two vectors; a starting point (a0, in the article) and a direction (a1, in the article). The max length you want to go is the parameter, t.
If you have the starting point and the direction you are going in as normalized vectors, then the position of the red dot equals,
RedDot = a0 + t*a1.
As an example, let's say the bottom box is at (0,0,0), and the direction you're shooting is (-1,1,0), and you want to find the red dot at a max length of 5, then the coordinates of the dot is RedDot = (0,0,0) + 5*(-1,1,0) = (-5, 5, 0).
It's that easy. :)
Sorry, only the direction you are going should be a normalized vector. The starting point should not.
Answer by vic83 · Nov 08, 2011 at 07:03 PM
thanks ! giving it a try on it now.
I still don't know how to implement it, since the variables are of different kinds. i don't think it would simply work like that :
RedDot.transform.position = transform.position*max_length;
anyway, giving a try right after the coffee :) thank you again, if you have further hints, go ahead :)
Your answer
Follow this Question
Related Questions
Check if object is inside a viewing radius 1 Answer
Raycasting at an offset angle? 4 Answers
Angle between Ray and Normal 1 Answer
Problem With Raycasthit Angle 0 Answers