# Enemy Rotation

**URL:** <https://forum.kirupa.com/t/enemy-rotation/262715>\
**Category:** programming\
**Created:** [June 9, 2008, 1:09pm UTC](https://forum.kirupa.com/t/enemy-rotation/262715 "2008-06-09T13:09:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jephz](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Jephz](https://forum.kirupa.com/u/Jephz)\
**Post date:** [June 9, 2008, 1:09pm UTC](https://forum.kirupa.com/t/enemy-rotation/262715/1 "2008-06-09T13:09:41Z")

</div>

Hey guys, I have a problem. I need the enemy to rotate in the direction of the player. In other words, as the player moves, the enemy will rotate in the direction of the players movement. I’m trying to make a real time battle system like Athalina Rpg/Hero rpg. Any hints?

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 9, 2008, 1:39pm UTC](https://forum.kirupa.com/t/enemy-rotation/262715/2 "2008-06-09T13:39:21Z")

</div>

search the forums you…

I kindly hint that you might just try to search the forums for simular examples/questions and tutorials

---

<div class="post-metadata">

**Author:** ![Jephz](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Jephz](https://forum.kirupa.com/u/Jephz)\
**Post date:** [June 10, 2008, 5:07am UTC](https://forum.kirupa.com/t/enemy-rotation/262715/3 "2008-06-10T05:07:11Z")

</div>

Ok…I found a code when searching on Google, the rotation works but the enemy is always facing the wrong way. Heres the code:

```auto
onClipEvent(enterFrame) {
var xd = _root.pc._x-_root.enemy._x;
    var yd = _root.pc._y-_root.enemy._y;
    var ang = Math.atan2(yd, xd);
    _root.enemy._rotation = ang*(180/math.PI);
} 

```

And heres the swf:  
[http://spamtheweb.com/ul/upload/100608/47199\_battle.php?lightbox](http://spamtheweb.com/ul/upload/100608/47199_battle.php?lightbox)

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 10, 2008, 6:13am UTC](https://forum.kirupa.com/t/enemy-rotation/262715/4 "2008-06-10T06:13:51Z")

</div>

Well… Think back to highschool. Remember trig? Look, when you position the hero directly north of him the enemy points left, so his rotation is at -90 because rotation in movie clips are oriented with up as 0. So, if you want to make -90 become 0, add 90 degrees. And just because your sig challenged me. Noob.

---

<div class="post-metadata">

**Author:** ![suvenier](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@suvenier](https://forum.kirupa.com/u/suvenier)\
**Post date:** [June 10, 2008, 2:29pm UTC](https://forum.kirupa.com/t/enemy-rotation/262715/5 "2008-06-10T14:29:11Z")

</div>

function focus(obj,mx,my,correction)  
{  
obj.rot = -Math.atan2((obj.\_y - my),(mx - obj.\_x ))\*180/Math.PI;  
obj.\_rotation = obj.rot-correction;  
}

obj=ENEMY  
mx= player.\_x  
my= player.\_y  
correction = type 0 normally, unless you can spot an angle error. you said your enemy faced the wrong way? keep typing in “correction” angles like 90, -90, 45, -45 … or change your enemy’s idle state.

---

<div class="post-metadata">

**Author:** ![Jephz](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Jephz](https://forum.kirupa.com/u/Jephz)\
**Post date:** [June 11, 2008, 1:24am UTC](https://forum.kirupa.com/t/enemy-rotation/262715/6 "2008-06-11T01:24:52Z")

</div>

Dude, I’m 13, still in middle school. Not really familiar with trig yet.

and suvenier, your code is too complicated for me, I’m still new to AS.

Fidodo, your the first to answer the challenge. You can have a cookie ^^.

Can anyone make things a little more simple for me to understand?

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 11, 2008, 2:54am UTC](https://forum.kirupa.com/t/enemy-rotation/262715/7 "2008-06-11T02:54:50Z")

</div>

[quote=Jephz;2340435]Dude, I’m 13, still in middle school. Not really familiar with trig yet.

and suvenier, your code is too complicated for me, I’m still new to AS.

Fidodo, your the first to answer the challenge. You can have a cookie ^^.

Can anyone make things a little more simple for me to understand?[/quote]

Ah, sorry man, sometimes I forget about that possibility.

---

<div class="post-metadata">

**Author:** ![Jephz](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@Jephz](https://forum.kirupa.com/u/Jephz)\
**Post date:** [June 11, 2008, 6:16am UTC](https://forum.kirupa.com/t/enemy-rotation/262715/8 "2008-06-11T06:16:13Z")

</div>

Ok thanks everyone, I got it fixed. I just added 90 degrees to the rotation, and now it works.
