Jump to content


Photo

Need Help with making an enemy


  • Please log in to reply
5 replies to this topic

#1 truemegamaniac

truemegamaniac

    GMC Member

  • New Member
  • 1 posts
  • Version:GM8

Posted 28 January 2012 - 07:24 AM

Hello, I am making an enemy for my game. It can already move towards my player, but there is one issue, it cant face the player, nor attack when the player is in range. All im sure i need is some way to check if an object is within a certain range, but no matter how many times i look up how to do it nothing useful comes up, or it's in GML which i tend not to bother with. So if anyone can give me a Drag and Drop method of checking to see if object_whatever is within a range that would be nice.
  • 0

#2 NukeTheCat

NukeTheCat

    GMC Member

  • GMC Member
  • 460 posts
  • Version:GM8.1

Posted 28 January 2012 - 08:20 AM

I've haven't used this yet, please tell if there is bugs
I think this code should work:
// you should place this in the Step Events
if collision_circle (xc, yc, radius, obj, prec, notme){
//action here
}
// this is the meaning of some: prec = precise? notme = count self? radius is measured by pixel

i don't think there is a D&D equivalent but I think there is a way by creating a invisible circle object and combine it with the enemy so to create an invisible border.

Sorry I'm still a noob just trying to be helpful :sweat:
  • 0

#3 Zefyrinus

Zefyrinus

    GMC Member

  • GMC Member
  • 148 posts
  • Version:GM6

Posted 28 January 2012 - 06:21 PM

Isn't there a D&D action like "check if instance exists at x,y" or something like that? So you use that action and make it check for the player at a position that is a little to the left or right of the enemy. If the player exists there, then the next action that you place after this one will be performed.
  • 0

#4 Joe H

Joe H

    GMC Member

  • GMC Member
  • 352 posts
  • Version:Unknown

Posted 28 January 2012 - 06:33 PM

Yeah, D&D has things like "Check Empty" or "Check Collision". However, a small bit of code would work best for this.

point_distance(x1,y1,x2,y2) Returns the distance between point (x1,y1) and point (x2,y2).


Adding this to the enemy (replace "player" with the name of your player's object):

EVENT: STEP
if (point_distance(x,y,player.x,player.y) < 200)
    {
    time_to_face_the_player = true
    }

  • 0

#5 Davido01

Davido01

    GMC Member

  • New Member
  • 905 posts

Posted 28 January 2012 - 06:37 PM

Or rather than making it 10x more complicated with D & D, you can use GML. Sounds like you are getting to the stage now where you need the extra flexibility provided by GML.

The collision circle way probably works but I always do this for checking distance ranges between objects.

Create:

weaponRange = 100

Step: (can also use an alarm so it checks every certain number of steps but that needs additional code)

if distance_to_object(objEnemy) <= weaponRange
{
    //shooting code
}

Edited by Davido01, 28 January 2012 - 06:38 PM.

  • 0

#6 Robotank

Robotank

    GMC Member

  • New Member
  • 40 posts
  • Version:GM:HTML5

Posted 28 January 2012 - 06:46 PM

You can use a Test Expression check of point_distance() to get the effect you are looking for.

CHECK EXPRESSION (point_distance(obj_enemy.x, obj_enemy.y, obj_player.x, obj_player.y) < attack_radius)
^
PUT
YOUR
ATTACK
BLOCKS
HERE
v

You can also use point_direction with similar arguments to find the angle between the enemy and the player.

HOW THIS WORKS:
CHECK EXPRESSION allows you to type in a line of code expecting that line to either be true or false. In this case, if the distance between the enemy and the player is less than your attack radius, then it is true and the attack code proceeds. If the expression evaluates as false, then the attack code is skipped and it continues on to the next steps.
Point_Distance(x1, y1, x2, y2) finds the distance from x1,y1 to x2,y2 using basic trig. You'll have to be a little clever to get the right x and y values as inputs and without knowing object names or how your game is set up, I can't really give you any accurate advice.

If you were curious, the equivalent GML looks really similar:
if (point_distance(obj_enemy.x, obj_enemy.y, obj_player.x, obj_player.y) < attack_radius) {
//attack code goes here
}

Let us know if this doesn't quite get you there and we can get you a bit more help :biggrin:

*EDIT*
Davido01's expression is even simpler. I had forgotten about distance_to_object. That's probably your best bet in either D&D or GML.

Edited by Robotank, 28 January 2012 - 06:48 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users