Page 1 of 1

Random Movement

PostPosted: Sun Aug 24, 2008 6:14 pm
by firerfun
Let me start off by saying im AWFUL with code. and I really badly need a code to make an enemy move randomly around a 2d area. The game is from a birds eye view if that changes anything. if anyone could do that for me it would be a BIG help.

I would ask for an explaination so i could learn how but im really slow when it comes to learning code.

Thanks!!!

Re: Random Movement

PostPosted: Sun Aug 24, 2008 7:43 pm
by tlah
this took me a while to figure out. Go into the enemy's actor control and select a draw actor function. use script editor to type the following code:
Code: Select all
MoveTo("Event Actor", rand(640.000000),rand(480.000000), 5.000000, "hero", "");

also, create an actor called "screen" (name: screen- type: wire-frame region) make it long and thin to fit on the ends of the view. clone it 3 times and make them fit on the other sides of the view ( like four rectangles outside of a rectangle) make a colision in "enemy", use physical response and in final velocity multiplyer make the top box -1. also, in another colision with the +screen, make a "move to" and make the x 10 and y 10, relative to the main actor (i used "hero")if u r confused, just ask! :D

Re: Random Movement

PostPosted: Mon Aug 25, 2008 12:27 pm
by asmodeus
The code above lets the enemy moving to a certain point in the view (I think).

If you'd like him to move in the direction up, down, left or right you can use this:
Code: Select all
int yourspeed = 2; // this is the speed the enemy has to move
switch ( (int)rand(4) )
{
  case 0: xvelocity = -yourspeed; yvelocity = 0; break; // the enemy moves to the left
  case 1: xvelocity = 0; yvelocity = -yourspeed; break; // the enemy moves to the left
  case 2: xvelocity = yourspeed; yvelocity = 0; break; // the enemy moves to the left
  case 3: xvelocity = 0; yvelocity = yourspeed; break; // the enemy moves to the left
}

To let him moving in any direction you can use this much easier code:
Code: Select all
var yourspeed = 2;
direcitonal_velocity = yourspeed;
angle = rand(360);

Re: Random Movement

PostPosted: Thu Aug 28, 2008 12:31 pm
by Bee-Ant
If the enemy would move randomly in angle and speed, here's the very short code
Code: Select all
angle=rand(360);
directional_velocity=rand(10)+1;

Re: Random Movement

PostPosted: Thu Aug 28, 2008 4:07 pm
by Spidy
Nice Codes Guys

Re: Random Movement

PostPosted: Wed Sep 03, 2008 8:38 am
by Troodon
Bee-Ant wrote:If the enemy would move randomly in angle and speed, here's the very short code
Code: Select all
angle=rand(360);
directional_velocity=rand(10)+1;


Hey guys, won't that make the direction change every now and then making the enemy just shake like crazy?
I think I had the problem once so I made the direction change in a random time interval. (a timer with for example 100ms-2000ms)
:)

Re: Random Movement

PostPosted: Wed Sep 03, 2008 1:53 pm
by Kalladdolf
or let the directional_velocity get less until it's reached zero and then speed it up again :mrgreen: