# Problems with homing in as3

**URL:** <https://forum.kirupa.com/t/problems-with-homing-in-as3/182353>\
**Category:** Uncategorized\
**Created:** [March 17, 2006, 11:14am UTC](https://forum.kirupa.com/t/problems-with-homing-in-as3/182353 "2006-03-17T11:14:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![maredsous](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@maredsous](https://forum.kirupa.com/u/maredsous)\
**Post date:** [March 17, 2006, 11:14am UTC](https://forum.kirupa.com/t/problems-with-homing-in-as3/182353/1 "2006-03-17T11:14:54Z")

</div>

I haven’t been able to figure out how to turn an enemy to correctly follow the user in a game i’m playing around with – take a look here:

flash player 8.5 required (as3) –

[http://www.danielhai.com/blog/wp-content/uploads/2006/03/Game1.swf](http://www.danielhai.com/blog/wp-content/uploads/2006/03/Game1.swf)

when I cross the 180 / -180 degree line, I can’t figure out how to make the enemies turn the correct way (they decide to go the long way)

I tried modding this script into something that looks like the algorithm here:

[http://www.gotoandplay.it/\_forums/viewtopic.php?t=1776&sid=6bda54a1a82304c54b78bced0b2ac7dd](http://www.gotoandplay.it/_forums/viewtopic.php?t=1776&sid=6bda54a1a82304c54b78bced0b2ac7dd)

but couldn’t get it to work right still – here’s what my code looks like:

```auto
 
private static const RADIANS:Number = 180 / Math.PI;
private static const PI2:Number = Math.PI * 2;
 
public function turnTo(object:RenderObject):void {
 
var ang:Number = getRotationRadians(object);
var angDiff:Number = ang - (_rotation / RADIANS);
 
(angDiff > Math.PI) ? angDiff -= PI2 : 0; 
(angDiff < -Math.PI) ? angDiff += PI2 : 0;
 
Math.abs(angDiff) > MAX_ROTATION ? angDiff *= MAX_ROTATION / Math.abs(angDiff) : 0; 
 
rotation += (angDiff * RADIANS);
 
}
 
public function getRotation(object:RenderObject):Number {
return Math.atan2(object.y - y, object.x - x) * RADIANS;
}
 
public function getRotationRadians(object:RenderObject):Number {
return Math.atan2(object.y - y, object.x - x);
}

```

any ideas?
