Targeting using raycast

Hello wise ones,

I am having a bit of trouble. I am trying to use a raycast from my main camera to determine where the player will shoot. The game I am building has a isometric view and the player is on the map (think XCOM). I want the player to be free to shoot anywhere, even if there is no enemy at the position where he wants to shoot. The code below works fine, until I try to shoot an enemy from the side (left to right). The raycast goes straight through the enemy and hits the ground (playerPlane) behind it. Because of the perspective I am using, the player now shoots to the left from the enemy instead of straight at it. I could probably solve this by seeing what exactly the ray hits, but I am not sure how to do this, nor how to tell the player to shoot at the centre of the object that the ray hit. Or could I just have a ray hit the ground from above instead of using the main camera?

Does anyone have a suggestion?

var playerPlane = new Plane(Vector3.up, transform.position);
	        	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	        	var hitdist = 0.0;
	        		   	        		   			
	      			  if (playerPlane.Raycast(ray, hitdist)) 
	      			  {	               						
	                 		var targetPoint = ray.GetPoint(hitdist);
	                 		var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);   
                                        shoot(targetPoint);

}

I tried the following code and that works, except for the fact that if the ray hits an enemy, the player shoots at the exact point where the ray hit, instead of at the centre of the enemy where I want to shoot, causing the player model to rotate on it’s axis, which it should not do.

How do I tell not to use the exact point, but the centre of the obect it hit, provided it was not the ground it hit?

                var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     
                      if (Physics.Raycast(ray, hit)) 
                      {                                    
                            var targetPoint = hit.point;
                            var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);   
                            shoot(targetPoint);

 }

have a look at the other properties of the RayCastHit: Unity - Scripting API: RaycastHit

you can get the transform of the object you’ve hit, you can then use it’s position as the target point. If you’re target’s origin position is set to the floor you can either offset it a little, or you could at a “targetPoint” var to the enemies and access that or have an empty gameObject child parented to the enemy gameObject (similar concept to a spawn point on a gun model) etc.

Solved it.

var planePosition = Vector3(transform.position.x, transform.position.y +2.5f, transform.position.z);
	   var playerPlane = new Plane(Vector3.up, planePosition);

This did the trick. I moved the plane up by 2.5f. I’m not sure how this will work when shooting at something which is positioned higher, but we’ll see.