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.
if(isWeaponOut)
{
//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.LookAt(posToFace);
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, Color.red);