Random Movement

You must understand the Game Editor concepts, before post here.

Random Movement

Postby firerfun » Sun Aug 24, 2008 6:14 pm

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!!!
firerfun
 
Posts: 14
Joined: Mon Nov 27, 2006 10:27 pm
Score: 0 Give a positive score

Re: Random Movement

Postby tlah » Sun Aug 24, 2008 7:43 pm

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
User avatar
tlah
 
Posts: 66
Joined: Sun Apr 06, 2008 12:10 pm
Location: In your head, readin' ur mind
Score: 2 Give a positive score

Re: Random Movement

Postby asmodeus » Mon Aug 25, 2008 12:27 pm

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);
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: Random Movement

Postby Bee-Ant » Thu Aug 28, 2008 12:31 pm

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;
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Random Movement

Postby Spidy » Thu Aug 28, 2008 4:07 pm

Nice Codes Guys
Image
VOTE FOR MY SIGNATURE PLEASE.......THANK YOU
User avatar
Spidy
 
Posts: 935
Joined: Tue Aug 14, 2007 1:28 pm
Location: http://www.inatonibai.com
Score: 38 Give a positive score

Re: Random Movement

Postby Troodon » Wed Sep 03, 2008 8:38 am

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)
:)
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: Random Movement

Postby Kalladdolf » Wed Sep 03, 2008 1:53 pm

or let the directional_velocity get less until it's reached zero and then speed it up again :mrgreen:
User avatar
Kalladdolf
 
Posts: 2427
Joined: Sat Sep 08, 2007 8:22 am
Location: Germany
Score: 120 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest