As: Main
Hey,
This is my first tutorial. In this tutorial you will learn:
1. Rotation towards the mouse.
2. Making a MC follow the mouse
Here is a smaple.
You will need to know:
1. For this tutorial you will simply need a basic understanding of AS.
Rotation Towards Mouse
Okay so here is the script we will use:
Code:
angle_in_radians = Math.atan2(_root._ymouse - this._y , _root._xmouse - this._x); angle_in_degrees = Math.round((angle_in_radians * 180/ Math.PI)); this._rotation = angle_in_degrees;
Lets break this down a little now then.
angle_in_radians is just our variable name.
Math.atan2 is just an atan which we need to use but it is in the form of y, x which is what we need to do.
_root._ymouse – this._y, This calculates what the mouses y position – the objects y position is equal 2.
_root._xmouse- this._x does the same but with the x position.
angle_in_degrees, this is just our variable name again but this time it is degrees.
Math.round((angle_in_radians * 180/ Math.PI)); . You don’t really have to understand this it is just a conversion. We are rounding up what the angle_in_radians time 180 divided by PI is.
this._rotation = angle_in_degrees; finally we are saying rotate this as much as angle in degrees.
Your object may be pointing not at the mouse but 90 degrees in a different direction. If so double click the object and rotate it 90degrees in that direction.
Making an MC follow the mouse, with smoothing (covered by ApatheticMark)
Code:
this._x += (_xmouse-this._x)/40; this._y += (_ymouse-this._y)/40;
This works by finding the distance between the MC and the mouse, which is (_xmouse-this._x), then moving a fraction (1/40 in this example) towards it (this._x +=). This is smoothed as the nearer the MC is to the object, the smaller the fraction, and therefore the less it moves. If you use this code, please change 40 to whatever number you want, but remember that the lower the number, the faster the MC moves.
------------
You could alternately use this script by me.
Code:
yDifference = Math.round( _root._ymouse - this._y);
y_Amount_to_Move_tank = Math.round( yDifference / 5 );
this._y = y_Amount_to_Move_tank + this._y;
It essentially does the same thing. I am not going to explain it as Mark already did that but you can see what it does. And then do it again for the x:
Code:
xDifference = Math.round( _root._xmouse - this._x);
x_Amount_to_Move_tank = Math.round( xDifference / 5 );
this._x = x_Amount_to_Move_tank + this._x;
Thanks,
Unknownfury