I have 2d turret that is suppose to follow a player as seen in the image.
I tried a couple ways to get this to work, but both ways aren’t giving me the desired results.
The first way flips my turret image horizontally, instead of letting it rotate in its original position.
public Transform target;
Quaternion rotation = Quaternion.LookRotation
(target.transform.position - transform.position, transform.TransformDirection(Vector2.up));
transform.rotation = new Quaternion (0, 0, rotation.z, rotation.w);
My second attempt worked perfectly, but the turret follows the mouse and not the player. Maybe I would have to change the Input.mousePosition part to something else, but I’m not sure at this point.
Vector3 diff = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
diff.Normalize ();
float rot_z = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (0f, 0f, rot_z - 90);
I’ve also tried the LookAt method, but that flips my turret image into a different position as well. Any help would be greatly appreciated.