- Home /
Raycasting how to find position on end of ray float distance
Ray ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, 5/*Float Distance*/)){
transform.position = /*vector 3 ( position at end of ray distance )*/
}
let say between me and float distance lies box if I would look at box I would get position
vector 3 (0,0,1)
I would achieve that with this code
Ray ray = Player.camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit; // declare the RaycastHit variable
if (Physics.Raycast(ray,out hit)) {
transform.position = hit.point
}
hit point would be vector 3 (0,0,1)
but if that box is still there I want to find the vector 3 (0,0,5) so it would go through box and on float distance would stop and look it's position
I hope I explained well what I need
if not I'll need to make a picture
done on request
$$anonymous$$ake a picture... XD I didn't understand after Vector3(0,0,1) and Vector3(0,0,5).
Answer by AlucardJay · Feb 28, 2013 at 02:16 PM
Check my answer here : http://answers.unity3d.com/questions/406636/3d-object-attach-to-mouse-1.html
If you don't have a collider to raycast against for depth reference, but you know the distance from the camera you would like the point to be, you can use Ray : http://docs.unity3d.com/Documentation/ScriptReference/Ray.html
#pragma strict
public var distance : float = 1.5;
function Update()
{
CastRayToWorld();
}
function CastRayToWorld()
{
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var point : Vector3 = ray.origin + (ray.direction * distance);
Debug.Log( "World point " + point );
Debug.DrawLine( Camera.main.transform.position, point, Color.red );
}
Distance is distance from the camera that the ray is to terminate, play with this value. The Debug.DrawLine should show where this point is in world space.
thanks
but as always I hate java had to convert it
Ray ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
PickUpObjectScript.x.transform.position = ray.origin + (ray.direction * 5);
I thank you very much still :)
Sorry about that, as you can see I just quickly modified this from the other answer for you without taking notice of your tag. D'oh! But yeah, looks like you had no problems converting. The Unity script reference would have been helpful too for that I hope. Happy Coding =]
Your answer
Follow this Question
Related Questions
Strange Behavior with Hit.point 1 Answer
Multiple Cars not working 1 Answer
any else way finding floating point 1 Answer
Distribute terrain in zones 3 Answers
[C# / Unity] Raycast Ignoring Self / Offsetting Instantiations? 0 Answers