Jump to content


Photo

Lock on an enemy


  • Please log in to reply
8 replies to this topic

#1 Bladestorm Games

Bladestorm Games

    GMC Member

  • GMC Member
  • 318 posts
  • Version:GM8

Posted 25 April 2012 - 02:46 AM

Pretty much what the tittle and description says, how can i make say; objHoverMine , find the neasrest objEnemyParent ,then THAT one it will follow even if another is near by as it travels toward the locked on enemy.

I hope this is enough details heh, thanks
  • 0

#2 The2Banned2One

The2Banned2One

    BANNED

  • GMC Member
  • 232 posts
  • Version:GM:Studio

Posted 25 April 2012 - 02:50 AM

Make it so that in the create event:
target = instance_nearest(x,y,objEnemyParent);

And then in the step event have it move towards target.
  • 1

#3 Bladestorm Games

Bladestorm Games

    GMC Member

  • GMC Member
  • 318 posts
  • Version:GM8

Posted 25 April 2012 - 05:11 PM

Make it so that in the create event:

target = instance_nearest(x,y,objEnemyParent);

And then in the step event have it move towards target.


Alright it works, but now I spawn (throw out) 3 objHoverMine , how can i make the 3 choose different objEnemyParent if there are more than 2 or 3 enemies?
  • 0

#4 The2Banned2One

The2Banned2One

    BANNED

  • GMC Member
  • 232 posts
  • Version:GM:Studio

Posted 26 April 2012 - 03:33 AM

Create a script called "findNearestEnemy" with the following code:
/* Code Originally by Davve, Modified by The2Banned2One */
var ins, _id;
_id = -1;

while (1)
{
   ins = instance_nearest(argument0,argument1,objEnemyParent)
   if (ins > 0)
   {
      if (ins.lockedOn = false)
      {
          _id = ins;
          break;
      }
      else
      {
          instance_deactivate_object(ins);
      }
   }
   else
   {
      break;
   }
}
instance_activate_object(object0);

return _id;

Then add the following to your objEnemyParent's create event:
lockedOn = false;

Then change the create event of your objHoverMine from:
target = instance_nearest(x,y,objEnemyParent);
To:
target = findNearestEnemy(x,y);

if (target != -1) target.lockedOn = true;
else instance_destroy(); //Destroy it because it couldn't find an enemy to lock onto

Try that out and tell me if it works.

Edited by The2Banned2One, 26 April 2012 - 03:37 AM.

  • 0

#5 Bladestorm Games

Bladestorm Games

    GMC Member

  • GMC Member
  • 318 posts
  • Version:GM8

Posted 26 April 2012 - 04:38 AM

___________________________________________
ERROR in
action number 1
of Create Event
for object objHoverMine:

In script findNearestEnemy:
Error in code at line 10:
if (ins.lockedOn = false)
^
at position 16: Unknown variable lockedOn


Also i have the look on occur at the animation end of the objHoverMine:

if sprite_index = sprHoverMine
    then
    {   
        target = findNearestEnemy(x,y);

        if (target != -1) target.lockedOn = true;
        else instance_destroy(); //Destroy it because it couldn't find an enemy to lock onto

        stage_1 = true // stage 1 is complete and does stuff in step event
        sprite_index = sprHoverMineActive // now the mine is active and will hunt down the eneimies
    }


Step event
if speed <= 0 and stage_1 = false
    {
        speed -=0.1
    }

if stage_1 = true and instance_exists(target) and distance_to_object(target) <=200
    {
        if stage_1_speed <= 6
            {
                stage_1_speed += 0.3
            }
        move_towards_point(target.x,target.y,stage_1_speed)
    }
    
if stage_1 = true and !instance_exists(target)
    then
    {
        instance_create(x,y,objTestExplosion1)
        instance_destroy()
    }

Edited by Faultekaiser, 26 April 2012 - 04:39 AM.

  • 0

#6 The2Banned2One

The2Banned2One

    BANNED

  • GMC Member
  • 232 posts
  • Version:GM:Studio

Posted 26 April 2012 - 04:44 AM

Then add the following to your objEnemyParent's create event:

lockedOn = false;


I think you forgot this part.
  • 0

#7 Bladestorm Games

Bladestorm Games

    GMC Member

  • GMC Member
  • 318 posts
  • Version:GM8

Posted 26 April 2012 - 05:01 AM


Then add the following to your objEnemyParent's create event:

lockedOn = false;


I think you forgot this part.

No it's in there. Isnt the error in the script?
  • 0

#8 The2Banned2One

The2Banned2One

    BANNED

  • GMC Member
  • 232 posts
  • Version:GM:Studio

Posted 26 April 2012 - 10:51 AM

The error is encountered in the script. The error is occurring because one of the objEnemyParent's it finds does not have a variable called lockedOn.

Edited by The2Banned2One, 26 April 2012 - 10:51 AM.

  • 0

#9 The2Banned2One

The2Banned2One

    BANNED

  • GMC Member
  • 232 posts
  • Version:GM:Studio

Posted 26 April 2012 - 10:44 PM

BUMP:
Just PM me your GMK and I'll see what I can do.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users