- Home /
find a world-space coordinate from ScreenPointToRay(Input.mousePosition) raycast
How can I find the World-Space coordinate from a raycast with a set distance, without hitting a collider?
Or : is there a proper way of finding a world space coordinate from a 2D input and a set distance from the camera? Example :
Player touches the screen or uses a mouse, either way the input is a screen coordinate.
Ray is cast at a distance of 10.0 :
if ( Physics.Raycast( Camera.main.ScreenPointToRay(Input.mousePosition), 10.0) ) {
even if there is no colliders, how can I get a return for the point that is at the end of the raycast? Thanks =]
I really hope there is a simple solution, without using the camera's transform and rotation, ins$$anonymous$$d with both Input.mousePosition and Input.GetTouch(i).position
Answer by ThermalFusion · Sep 07, 2012 at 09:26 PM
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 point = ray.origin + (ray.direction * distance);
Thankyou, this is exactly the kind of thing I was looking for, but didn't know Ray even existed. This has been most helpful (upvote to you!) : http://docs.unity3d.com/Documentation/ScriptReference/Ray.html
Here is a JS version for anyone else who may read this :
#pragma strict
public var distance : float = 4.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 );
}
Answer by Hesher · Jun 24, 2014 at 09:28 AM
"var point : Vector3 = ray.origin + (ray.direction * distance); "
is equal to:
"var point : Vector3 = ray.GetPoint(distance);"
Your answer
Follow this Question
Related Questions
Vector3.Distance between camera and raycasthit 2 Answers
Keep camera at certain distance from character when rotating 3 Answers
Set Distance to object 1 Answer
Vector3(x,y,variable)? 0 Answers