ForumsProgramming ForumHow to make an object point at the mouse?

9 5965
yayformeee
offline
yayformeee
136 posts
Nomad

I have problems with this, I'm not sure if I'm using the proper script.
here it is.
onClipEvent (enterFrame) {
dy = _ymouse-this._y;
dx = _xmouse-this._x;
radius = Math.sqrt(dy*dy + dx*dx);
degrees = Math.atan2(dy,dx);
this._rotation = degrees*Math.PI/180;
}

  • 9 Replies
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

While I can't fix your code, I can get you one that works for sure, luckly I have answered this one before and I just need some copy and paste

First you'll have to code the angle in degrees your character is shoting, which we'll obtain from atan2, tan from tangent

http://img79.imageshack.us/img79/5020/atan2he4.jpg

So code something like this

rotationinradians = Math.atan2(_ymouse - Your Arm/Gun._y , _xmouse - Your Arma/Gun._x);


The problem is, this will give you the value in radians, and we want degrees, so a litle more math to convert it

rotationindegrees = rotationinradians * 180 / Math.PI;


This is on Flash Help

Then just make your object._rotation the same value as rotationindegrees

MyObject._rotation = rotationindegrees;


That should solve it
yayformeee
offline
yayformeee
136 posts
Nomad

onClipEvent (enterFrame) {
dy = _ymouse-this._y;
dx = _xmouse-this._x;
radianrotation = Math.atan2(dy,dx);
degreerotaition = radianrotation *180 / Math.PI;
this._rotation = degreerotation;
}
Is this what you mean?

The turret goes all weird.
When I move the mouse directly away from it, it spins in random directions.

LonLonRanch
offline
LonLonRanch
172 posts
Nomad

It might be a problem with the registration point of your movie clip. Make sure the registration point (the little + sign) of your MC is where you want the movieClip to rotate, and not in the center.

LonLonRanch
offline
LonLonRanch
172 posts
Nomad

Yea, I just tested it with this:

onClipEvent (enterFrame) {
dy = _root._ymouse - this._y;
dx = _root._xmouse - this._x;
radianrotation = Math.atan2(dy,dx);
radianrotation = radianrotation * 180 / Math.PI;
this._rotation = radianrotation;
}

and it worked for me, so make sure your registration point is located in the right position. All you have to do is go inside the movieClip and move the graphic so that the desired pivot point is located in the location you wish to rotate.

Cowmaster100
offline
Cowmaster100
37 posts
Nomad

Make sure its registration point is set to the center and that it is facing the right. Give it an instance name of âmanâ.

Now place this code on the frame



function convert(radians:Number):Number {
degrees = radians*(180/Math.PI);
return degrees;
}
man.onEnterFrame = function() {
var adjacent:Number = this._x-_xmouse;
var opposite:Number = this._y-_ymouse;
var angle:Number = Math.atan2(opposite, adjacent);
this._rotation = convert(angle);
};

Cowmaster100
offline
Cowmaster100
37 posts
Nomad

Make sure its registration point is set to the center and that it is facing the right. Give it an instance name of �man�.

i mean make it's instance name
man
Zrb
offline
Zrb
8 posts
Nomad

Try this code:
onClipEvent (enterFrame) {
_rotation = ((Math.atan2(_root._ymouse-_y,_root._xmouse-_x)/(Math.PI/180))+90
}
Make sure your gun movie clip is facing upwards.

choalejo
offline
choalejo
110 posts
Peasant

wow i have never heard of this

Voidcrystal
offline
Voidcrystal
138 posts
Nomad

How to make it don't go upside down when it turns on the other way? I know the script for normal:

onEnterFrame = function (){

But i don't know for OnClipEvent!


Here is the script for OnEnterFrame...

if (this._rotation < -90 || this._rotation > 90)
{
this._xscale = -Math.abs (this._xscale);
this.dir = -1;
this._rotation -= 180;

Don't seem to work with OnClipEvent...

Showing 1-9 of 9