- Home /
How to determine multiple points along a ray?
How do i determine multiple points along a ray?
Thanks-
here is my original question http://answers.unity3d.com/questions/481910/instantiate-wallfence-down-a-raycast-from-point-a.html
Answer by robertbu · Jun 27, 2013 at 02:02 AM
A Ray is composed of an origin and a direction. In addition, the direction is always normalized. So the best way is to use Ray.GetPoint() which will find a point a specified distance along the ray. This is equivalent to:
var v3Pos = ray.origin + ray.direction * distance;
If you have a figurative ray (i.e. an origin and a direction but not in a Ray class instance), you can use the above formula if you make sure the direction is normalized.
Im trying to get multiple points on a ray based on a distance between 2 marker points( Point A and Point B )
From $$anonymous$$arker A, i want to Shoot a ray to $$anonymous$$arker B. Then I want to Instantiate Wall Segments (10 units in width) along that ray from marker A to $$anonymous$$arker B.
The amount of Wall segments that are needed are based on the distance i have to cover. So say a distance of 50 units, Thats 5 walls segments based on a wall segment being 10 units wide.
This answer has four ways of moving a specific distance along a vector:
http://answers.unity3d.com/questions/478199/finding-the-direction-an-object-is-moving-and-givi.html
As for your specific problem, assume you have two Vector3 variables: v3A, and v3B that are the positions of your two objects.
You can build a ray from the two like this:
var ray = new Ray(v3A, v3B - v3A);
And then you can use GetPoint() above to find a point at a certain distance.
Or you can do the calculations yourself:
var origin = v3A;
var direction = (v3B - v3A).normalized;
var v3Pos = origin + direction * distance;
So v3A and v3B are the transforms of marker A and $$anonymous$$arker B?
v3A and v3B are the Transform.position of the two markers. If you have transforms, you can just replace v3A with trA.position and v3B with trB.position where trA and trB are the transforms of the markers.