### Mouse follower with constant speed (zombie)

Adds speed * the normalized vector between mouse and
me, the object to follow. Place the scripts
inside the object to follow the mouse.
**frame 1:**
speed = 5;
**frame 2:**
// nothing in here
**frame 3:**
angle = Math.atan2 ( _root._xmouse - _x, _root._ymouse - _y);
_x += speed * Math.sin ( angle );
_y += speed * Math.cos ( angle );
gotoAndPlay(2);

- Teis Date: 12/07/2001

Thanks for a very need site.
Comment: When the object reaches the mouse, it begins to flicker. Proberbly a very little modification of the code would help this.

- Navneet Behal Date: 23/07/2001

Thanks Ralf,
As far as the jumpiness is concerned, well I made a few tests and realized that there was an inaccuracy factor of about 1px and if we are willing to sacrifice that 1 pixel offset.. the shake goes away.
Now, in my test, the inaccuracy was 1px, but it might be less or more in your case, so I made it into a prototype with an optional argument to enter the inaccuracyFactor.. default is one..
Also, in keeping with Ralph\'s original speed number, the speed default if 5, but can be entered in the arguments.
Here goes the prototype
-------------------------------------
Movieclip.prototype.constantSpeedMotion=function(destinationX,destinationY,speed,inaccuracyFactor){
if(speed==undefined){
var speed=5;
}//End if
if(inaccuracyFactor==undefined){
var inaccuracyFactor=1;
} else {
var inaccuracyFactor=Math.round(inaccuracyFactor);
}//End if
if(Math.abs(destinationX-Math.round(this._x))<=inaccuracyFactor || Math.abs(destinationY-Math.round(this._y))<=inaccuracyFactor){
return(true);
}//End if
var angle=Math.atan2(destinationX-this._x,destinationY-this._y );
this._x += speed*Math.sin(angle);
this._y += speed*Math.cos(angle);
}//End function
-------------------------------------
Usage example:
If you want it to be a mouse follower, add this to mc you want to follow the mouse
-----------------------------------
onClipEvent (enterFrame) {
this.constantSpeedMotion(_parent._xMouse,_parent._yMouse);
}//End enterFrame
//or to make reach a specific target of let\'s say, 300 on x and 200 on y
onClipEvent (enterFrame) {
this.constantSpeedMotion(300,400,10,1);
}//End enterFrame
-----------------------------------
Enjoy :)
Navneet

- Muhammad Date: 17/09/2001

i want a mouse follower please send it for frre please

- kie Date: 18/02/2002

Nice stuff...
- Bob Date: 15/02/2003

I feel very stupid asking this question, but I am just beginning to use html text. When you say just plug in what you have into the object you want to follow the mouse, what do you mean?
- bruno Garcia Date: 19/03/2005

If think I got a simpler idea for the problem, it's the same idea of the accuracy factor...
frame 1
onClipEvent (load){
speed=5
}
frame 2
nothing
frame3
onClipEvent(enterFrame){
tangx =_root._xmouse-_x
tangy =_root._ymouse-_y
tang = (tangx+tangy)/(_root._xmouse+_root._ymouse);
if (Math.abs(tang)<=0.05){
stop ();
} else{
angulo =Math.atan2(tangx,tangy);
_x += Math.sin(angulo)*speed;
_y += Math.cos(angulo)*speed;
gotoAndPlay (2);
}
have fun
bruno
- PET WUSSY Date: 01/10/2007

YOU AALL NEED LIVES. GET A LIFE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ah!!!!!!!!
Add comment

Home