- Home /
Question by
RaulG · Aug 08, 2013 at 12:58 AM ·
raycastmouseposition
Trying to get the mousePosition in space.
The EXACT mousePos.
The one I currently have is off by a bit which makes it difficult to do things:
Vector3 mousePos = Input.mousePosition;
mousePos.Normalize();
Ray HookRay = new Ray(HookChild.position,mousePos);
Debug.DrawRay(HookRay.origin,HookRay.direction,Color.blue);
Comment
If you want the exact position, why are you normalizing the value?
Because it gives me the closest so far. But it's not right.
Answer by robertbu · Aug 08, 2013 at 04:04 AM
If you are looking to create a ray, you should use Camera.ScreenPointToRay().
Ray HookRay = Camera.main.ScreePointToRay(Input.mousePosition);
Debug.DrawRay(HookRay.origin,HookRay.direction,Color.blue);
This will create a ray into the scene at the mouse position starting at the camera's near clip plane.
That is from the camera, I'd like for it to come from the object itself.
From the object to where? If it is from the object in the same direction, then just change the origin.
Ray HookRay = Camera.main.ScreePointToRay(Input.mousePosition);
HookRay.origin = transform.position;
Debug.DrawRay(HookRay.origin,HookRay.direction,Color.blue);
Or if you want a ray back from the object towards the camera:
Ray HookRay = Camera.main.ScreePointToRay(Input.mousePosition);
HookRay.origin = transform.position;
HookRay.direction = -HookRay.direction;
Debug.DrawRay(HookRay.origin,HookRay.direction,Color.blue);