ScreenPointToRay alternatives?

Ok guys so heres the situation…
I’m taking a point from my 3rd person game, casting a ray (from the camera), and using that for figuring out where my bullet is gonna go, I’m also using this for facing. I am running into one main problem and I was wondering if someone can steer me in the right direction. I would have searched this on google but I’m not sure what to even call what I’m looking for…

Problem - if a object is in between the player, and the camera - the raycast of the bullet hits the object…

This is a small snip of how I am getting the facing, and the ray using the ScreenToWorldPoint, but I was wondering if there is a better, or more true method. The only work around I can think of is imposing some type of distance to ignore the ray at, giving it perhaps a range (my zoom on the player is static) but I was wondering if anyone had a better solution Or make the camera bump forward if a object does go between the player and the camera… both sound like odd solutions to me. So yea, all I can think of is 2 I guess.

			//Position to look towards if weapon is drawn from holster
			posToFace = camera.ScreenToWorldPoint (new Vector3(Screen.width*0.5f,Screen.height*0.5f,Mathf.Max (camera.nearClipPlane,Vector3.Distance (player.transform.position,camera.transform.position))));
			//Working with ScreenToWorldPoint
			player.transform.localRotation = Quaternion.Euler(new Vector3(0.0f, player.transform.localRotation.eulerAngles.y,0.0f));
			//Raycaster declared for shooting
			ray = camera.ScreenPointToRay (new Vector3(Screen.width*0.5f,Screen.height*0.5f,Mathf.Max (camera.nearClipPlane,Vector3.Distance (player.transform.position,camera.transform.position))));
			Debug.DrawRay(ray.origin,ray.direction *400,;

I’m a bit confused on how you’ve structured your game. It appears that aiming and shooting comes from the middle of the screen, but the 3rd person character looks towards the point where the bullets will go. Some ideas/observations:

  • You can use Collider.Raycast() to do a raycast against a single object such as a background plane.
  • You can use Physics.RyacastAll() to get a list of colliders hit by a ray (i.e. it does not stop at the first one). The list is not sorted, so you would have to cycle through the list looking either for an object by name or at a certain distance.
  • You can use Plane.Raycast(). This is a mathematical plane, not a physical plane, so you don’t need a physical object to do this kind of Raycast().
  • The ‘Z’ parameter of the Vector3 passed to ScreenPointToRay() is ignored, so you don’t have to calculate it.
  • Using the player position for your ScreenToWorldPoint() might be good enough, but it has issues. It would be better to a Plane.Raycast() with a mathematical plane through the character using the camera’s ‘-transform.forward’ for the normal.
  • Since you are casting from the center of the screen you can use ‘transform.position + transform.forward’ * dist’ as an alternate to ScreenToWorldPoint().

Try drawing your ray from the end of the gun instead of the camera, pointing along the gun’s forward vector

Ray gunRay = new Ray(gun.transform.position, gun.transform.forward);
Debug.DrawRay(gunRay.origin, gunRay.direction*400,;