- Home /
Getting an object to sit along a raycast
I am using a Kinect and trying to get an object to sit at a certain distance along a raycast.
_x = _hit.point.x;
_y = _hit.point.y;
_z = rhandpos.z;
//_z = _hit.collider.transform.position.z;
//Debug.Log("cross z pos" + _z);
cross.transform.position = Vector3.Lerp(cross.transform.position,new Vector3(_x,_y,_z + -10.0f),Time.smoothDeltaTime);
what I have is the crosshair at the raycast hit.y and .x but it dosent give an accurate representation of the ray cast any advice would be great
Are you saying you would like your cross.transform.position result to be 10 units along the line connecting the current cross.transform.position and _x, _y, _z?
i need it to be perfectly in line with the raycast at any distance i specify along the raycast
Answer by aldonaletto · May 23, 2012 at 04:11 AM
If you generated the raycast with a ray, use this:
cross.transform.position = ray.GetPoint(hit.distance - offset);
where ray is the ray used in Raycast, and offset is the distance from the hit point.
If you generated the raycast with a starting point and a direction vector, use this:
cross.transform.position = hit.point - dir * offset;
where dir is the unit vector that defined the raycast direction, and offset the distance from the hit point.
Thanks for your help, almost there but the crosshair travels towards the player then resets to the end of the raycast and repeats??
Where's the player? At the raycast origin? If you're using a ray you may have problems with the first alternative and negative distances (offset > distance from ray origin). You could use a modified version of the second alternative (uses ray.direction ins$$anonymous$$d of dir):
cross.transform.position = hit.point - ray.direction * offset;
Your answer
Follow this Question
Related Questions
FPS shooting 1 Answer
Drag Rigid Bodies First Person Controller.js 0 Answers
Display issues with GUI only showing details for last object in list. 2 Answers
Raycast Positioning 2 Answers
Question About Ray casting in an Area and Perspective 2 Answers