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);
}