- Home /
Raycast won't hit Collider
I trying to build a point and click system where the point i click will become destination point for the NavMeshAgent. Here is the code i am using:
if (Input.GetKey (KeyCode.Mouse0)) {
ray = characterCamera.ViewportPointToRay (Input.mousePosition);
didHit=Physics.Raycast (ray, out hit);
Debug.Log (didHit.ToString ());
Debug.DrawRay (ray.origin,ray.direction,Color.red,30f);
if (didHit) {
rayHitPosition=hit.point;
currentMarker=Instantiate (Marker,rayHitPosition,Quaternion.Euler (Vector3.zero));
}
My floor is a basic plane with a plane collider. This code always shows didHit false even if i am obviously pressing on the plane. Even Debug.DrawRay doesn't draw anything even though debugging shows ray variable does contain a valid ray. characterCamera is my main camera.
Answer by GameVortex · Dec 06, 2013 at 12:23 PM
The viewport space is the normalized screen space, meaning that the bottom left of the screen has the coordinates (0,0) and the top right has (1,1). The mousePosition uses screen space which is from (0,0) to (Screen.width, Screen.height).
Try using ScreenPointToRay instead:
ray = characterCamera.ScreenPointToRay(Input.mousePosition);
Thanks works perfectly now. I didn't notice there was a screen and viewport options.
Your answer
Follow this Question
Related Questions
Shoot off multiple raycasts from 1 object? 2 Answers
set ray.point as x, y and z float 1 Answer
How to place an object in the direction of a raycast 1 Answer
Raycasting and foreach loops 1 Answer