I apply transform.LookAt on my player’s gameObject to face one enemy gameObject and then another, and the player ends up facing the wrong side in both cases.
Below I pasted the code and a screen capture of the editor view seen from above, with text explaining what happens when my player looksAt one enemy and when it looksAt the other one.
I hope it helps understand what’s going on.
I’ve seen a response from @aldonaletto to another similar question and other similar questions about LookAt, but I don’t know how to apply them to solve my particular case.
This is the code:
Ray ray = viewCamera.ScreenPointToRay (Input.mousePosition);
Plane groundPlane = new Plane(Vector3.forward, Vector3.forward * gunController.GunHeight);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance)) {
Vector3 point = ray.GetPoint(rayDistance);
crosshairs.transform.position = point;
RaycastHit hit;
if (crosshairs.DetectTarget(ray, gunController.equippedGun.range, out hit)) {
//if target detected at x/y/z, aim at it
gunController.Aim(hit.collider.transform.position);
}
else {
//if no target detected in z axis, aim at x/y/0
gunController.Aim(point);
}
}
Any ideas anyone?
Thanks!