rotate gameobject following another gameobject with animated

GameObject A rotate and follow to target gameobject.

Gun Tower shoot follow enemy. It’s 2D graphic. How to make it ?

You can use Mathf.Atan2 to find the angle between the gun and the target. And then just display that angle in whatever way you do in your game — selecting the appropriate sprite image, for example.

Thank you JoeStrout! Can you have example code ?

Something like this…

float angle = Mathf.Atan2(targetPos.y - myPos.y, targetPos.x - myPos.x);
Debug.Log("Angle to target: " + angle);
spriteRenderer.sprite = mySprites[mySprites.Count * angle/360];

Details will obviously depend on how your game is set up, but that’s the basic idea.

Thanks you! But angle is float. mySprites is Int. How to fix it ? Please!

Ah yes, I guess you need to typecast, like this: mySprites[(int)(mySprites.Count * angle/360)]