# Rotating Towards Mouse

**URL:** https://forum.kirupa.com/t/rotating-towards-mouse/215913
**Category:** programming
**Created:** [February 12, 2007, 3:45am UTC](https://forum.kirupa.com/t/rotating-towards-mouse/215913 "2007-02-12T03:45:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Holmesc](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@Holmesc](https://forum.kirupa.com/u/Holmesc)
#### Post date: [February 12, 2007, 3:45am UTC](https://forum.kirupa.com/t/rotating-towards-mouse/215913/1 "2007-02-12T03:45:49Z")

</div>

I’m trying to create a turret that will rotate so that it is pointing at the mouse. Without the knowledge to make it happen, I sifted through tutorials until I found some code, which I adapted to my circumstances. However, the code was made to rotate towards an MC that only moved horizontally, which may be why it doesn’t work… Anyhow, here’s the code:  
ActionScript Code:  
[FONT=Courier New][LEFT][COLOR=#0000FF]onEnterFrame[/COLOR] = [COLOR=#000000] **function** [/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
angle = [COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]atan2[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000FF]\_root[/COLOR].\_xmouse-[COLOR=#0000FF]\_root[/COLOR].[COLOR=#000080]turret[/COLOR].[COLOR=#0000FF]\_x[/COLOR], [COLOR=#0000FF]\_root[/COLOR].[COLOR=#0000FF]\_ymouse[/COLOR], [COLOR=#0000FF]\_root[/COLOR].[COLOR=#000080]turret[/COLOR].[COLOR=#0000FF]\_y[/COLOR][COLOR=#000000])[/COLOR]/COLOR=#000000[/COLOR];  
[COLOR=#0000FF]\_root[/COLOR].[COLOR=#000080]turret[/COLOR].[COLOR=#0000FF]\_rotation[/COLOR] = angle;  
[COLOR=#000000]}[/COLOR];  
[/LEFT]  
[/FONT]

Yes, relatively simple code, but as I’m not the greatest at math, I need a little help straightening this out. Thanks for your help!
