Coldest
Coldest

Reputation: 21

Game Maker - Can't create tracking projectiles

Hey guys I'm developing a RTS game in Game Maker and I need some help with creating tracking missiles.

The idea is to create an object that tracks in real time a given instance of an object unit. To put it simple, create a projectile that follows an enemy unit. Like a homing missile.

At the moment, whenever I create an obj_projectile I set a variable in the projectile itself "trackId" with the unit.id I'm tracking. This way:

projectile.trackId = foe.id;

Being foe the enemy unit I'm shooting.

And then, in the step event for the projectile:

//If unit does not exists keep going to the same point
if object_exists(trackId){
    lastKnownX = trackId.x;
    lastKnownY = trackId.y;
}

direction = point_direction(x, y, lastKnownX, lastKnownY);
image_angle = point_direction(x, y, lastKnownX, lastKnownY);
motion_add(point_direction(x, y, lastKnownX, lastKnownY), acceleration);

//Don`t go topspeed
if (speed > movespeed) speed = movespeed;

movespeed and acceleration are variables of the obj_projectile.

I tried to failsafe the references to the target unit in the first if by doing this:

if object_exists((trackId)){
    lastKnownX = (trackId).x;
    lastKnownY = (trackId).y;
}

But it doesn't work either.

Thing is, the result I'm having is that the projectiles are being created and then go the point the target was in that moment, and then stay there motion-less. Not tracking the target.

I really don't know what I'm doing wrong and I tried a ton of ways of doing this. Google did not prove useful this time. Not saying that Google doesn't has the answer but I just couln't find it :(

I'm doing this in a very wrong way? is there an obvious thing I missed? I'm not familiar completely to refering to objects by it's ID, so it may be that.

Thanks for reading.

Upvotes: 1

Views: 183

Answers (1)

Coldest
Coldest

Reputation: 21

Question was answered in another site, just for the record, I was using object_exists wrong, I needed to use to use instance_exists.

In the step event for the projectile:

if instance_exists(trackId){ 

instead of

if object_exists(trackId){

Upvotes: 1

Related Questions