Page 1 of 1

Having an Actor/Spaceship follow mouse at varying speeds

PostPosted: Sun Apr 10, 2005 5:46 pm
by DavidBeoulve
On CreateActor, I create a timer that ticks every 100 milliseconds, or every 10th of a second.

When that timer ticks, I run a script on that actor (in this case, the player's ship)

Remember, you must create the timer on the actor in question. You must create the custom variables I use here in the script editor or they will be "undeclared."

// double distance(double x1, double y1, double x2, double y2)
// Returns distance between points (x1, y1) and (x2, y2).
// double direction(double x1, double y1, double x2, double y2)
// Returns direction between points (x1, y1) and (x2, y2) (in degrees, 0 to 360, from positive x axis, counterclockwise, in degrees).

// first we get where the mouse is, taking into account the size of the view, or this doesn't work right:
MOUSE_X = xmouse - (view.width/2);
MOUSE_Y = ymouse - (view.height/2);

// Then we find the distance...
DISTANCE_TO_MOUSE = distance(x,y,MOUSE_X,MOUSE_Y);

// and set the angle of the actor to move towards the mouse
angle = direction(x,y,MOUSE_X,MOUSE_Y);

// now we change how fast the spacecraft moves towards the mouse based on how far away it is from the mouse.
if (DISTANCE_TO_MOUSE > 200)
{
diretional_velocity = 30;
} else if (DISTANCE_TO_MOUSE > 150 && DISTANCE_TO_MOUSE <= 200)
{
diretional_velocity = 25;
} else if (DISTANCE_TO_MOUSE > 100 && DISTANCE_TO_MOUSE <= 150)
{
diretional_velocity = 20;
} else if (DISTANCE_TO_MOUSE > 50 && DISTANCE_TO_MOUSE <= 100)
{
diretional_velocity = 15;
} else if (DISTANCE_TO_MOUSE <= 20 && DISTANCE_TO_MOUSE > 50)
{
diretional_velocity = 5;
} else if (DISTANCE_TO_MOUSE <= 20)
{
diretional_velocity = 0;
}

// viola. Alter the numbers to take into account the size of your ship graphic -- I bet there's a "Get Width of Actor" function but I don't know it.

It's easy to do. I suck at coding in GE, but this works.

Re: Having an Actor/Spaceship follow mouse at varying speeds

PostPosted: Mon Apr 11, 2005 7:51 pm
by makslane
DavidBeoulve wrote:On CreateActor, I create a timer that ticks every 100 milliseconds, or every 10th of a second.

When that timer ticks, I run a script on that actor (in this case, the player's ship)


You can use the Draw Actor event instead

PostPosted: Mon Apr 11, 2005 11:12 pm
by DavidBeoulve
Ah, thank you :)

I have changed it in my game in progress.

"directional_velocity" miss-spelled? It runs in the game, I copied that variable from the pop-up variable list in GE.

PostPosted: Tue Apr 19, 2005 2:51 pm
by ingsan
Nice coding, David !
Better than simply having the spaceship "sticking" on the mouse pointer :P Thanks