Jump to content


Photo

Moving Enemy Towards Player


  • Please log in to reply
7 replies to this topic

#1 Koolman11

Koolman11

    GMC Member

  • New Member
  • 26 posts
  • Version:GM8

Posted 07 August 2012 - 08:13 PM

Hello, i am new and i am making a side scrolling zombie shooting game and i would like my zombie to walk towards player, and please write it in code, i prefer it, oh and i already added gravity so that is no problem, oh and the code so when it runs into the wall how to i make it change direction. and one more thing; how do i get the zombie to stop at player and attack him

create event:

speed = 1
direction = 180
gravity = 0.6 //this is all i have so far, it makes him go left

this is collision eff:

if vspeed>0 //If the player is falling
{
    move_contact_solid(270,32); //Move the player down until it connects with the ground
}
if vspeed<0 //If the player is rising
{
    move_contact_solid(90,32); //Move the player up until it connects with the ceiling
}

vspeed=0 //Stops the player from continuing its movement

Edited by Koolman11, 07 August 2012 - 08:14 PM.

  • -2

#2 Koolman11

Koolman11

    GMC Member

  • New Member
  • 26 posts
  • Version:GM8

Posted 07 August 2012 - 08:56 PM

Help Sum1 Please!?
  • -3

#3 @Alex@

@Alex@

    Retired GMC Reviewer

  • Reviewer
  • 3049 posts
  • Version:Unknown

Posted 07 August 2012 - 09:09 PM

What a run-on sentence , I'm quite bad for using them myself but you remind me a rather hyperactive friend with how your topic reads.

There a numerous ways to get the Zombie to move towards the player, all rely on knowing the position of the player. We can get variables from an object using the syntax object.variable , therefore we can get the (x,y) coordinates by using obj_player.x and obj_player.y. Now I'm just going to do precisely what you ask for and not anything more advanced like pathfinding.

//We are close to the player so chase them
if (distance_to_object(obj_player) < 128)
	{
	//But not close enough to attack
	if (distance_to_object(obj_player) >16)
    	{
    	//Discover there direction we need to go in
    	if (obj_player.x > x)
        	{
        	Dir = 1 ;
        	x += 2 ;
        	}
    	else
        	{
        	Dir = -1;
        	x -= 2 ;
        	}
    	}
	else
    	{
    	//Stop and Attack in Here, by virtue of skipping the nested if statement you and moving via iterating the x variable we don't need to do anything to stop only to attack
    	
    	}
	}
else
	{
	if !place_meeting(x+2*Dir,y,obj_wall)
    	{
 		x += 2*Dir ;
    	}
	else
    	{
    	Dir *= -1 ; 
    	}
   
	
	}

Now this will move toward the players x value while you are within a range , You should probably do a place_meeting check before moving in the first two moving parts.


Edit : I see in the time I wrote this up you bumped your topic blatantly ignoring the rules. You may only bump, that is to post immediately after one of your posts, every 48 hours. Please respect the rules.

Edited by @Alex@, 07 August 2012 - 09:12 PM.

  • 0

#4 Koolman11

Koolman11

    GMC Member

  • New Member
  • 26 posts
  • Version:GM8

Posted 07 August 2012 - 10:09 PM

thank you, im going to try this out!
  • 0

#5 Koolman11

Koolman11

    GMC Member

  • New Member
  • 26 posts
  • Version:GM8

Posted 07 August 2012 - 10:15 PM

i got an error here for dir as an unknown variable:

if !place_meeting(x+2*Dir,y,o_wall)

  • -2

#6 @Alex@

@Alex@

    Retired GMC Reviewer

  • Reviewer
  • 3049 posts
  • Version:Unknown

Posted 07 August 2012 - 10:28 PM

I just told you off for bumping illegally, so what do you do? you bump illegally. You didn't declare the Dir variable , I don't think this is something that needs explaining declare it as either 1 or -1 in the create event of your object.
  • 0

#7 Koolman11

Koolman11

    GMC Member

  • New Member
  • 26 posts
  • Version:GM8

Posted 07 August 2012 - 10:38 PM

thank you, but what do i edit other than the names of the objs? my zombie just keeps going right...

speed = 1
gravity = 0.6

//We are close to the player so chase them
if (distance_to_object(o_player) < 128)
        {
        //But not close enough to attack
        if (distance_to_object(o_player) >16)
        {
        //Discover there direction we need to go in
        if (o_player.x > x)
                {
                Dir = 1 ;
                x += 2 ;
                }
        else
                {
                Dir = -1;
                x -= 2 ;
                }
        }
        else
        {
        //Stop and Attack in Here, by virtue of skipping the nested if statement you and moving via iterating the x variable we don't need to do anything to stop only to attack
        
        }
        }
else
        {
        Dir = 1
        if !place_meeting(x+2*Dir,y,o_wall)
        {
                x += 2*Dir ;
        }
        else
        {
        Dir *= -1 ; 
        }
   
        
        }

Edited by Koolman11, 07 August 2012 - 11:00 PM.

  • 0

#8 Koolman11

Koolman11

    GMC Member

  • New Member
  • 26 posts
  • Version:GM8

Posted 08 August 2012 - 02:55 AM

can someone please help me out i cant figure out what to add to this code i dont kno at all :( :sweat:

srry for the bump people
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users