(Solved) Logics on Bullets from Towers in a Tower Defence Game

I am currently making a tower defence game and I faced a few problems to meet my own requirements on the bullets shot by the towers in my game.
What I expect:
The tower select the nearest enemy (Enemy A) to be the shooting target and shoot a bullet towards Enemy A.

  1. The bullet will lock onto Enemy A and keep moving towards it whatever happens (Enemy A died or no longer the nearest enemy to the tower).
  2. The bullet must hit the Enemy A whenever Enemy A goes, except Enemy A died before the bullet touches it.
  3. If Enemy A died before the bullet touch Enemy A, the bullet will still move towards the last position of Enemy A and delete itself.

What I did:

  1. Version 1 (pick the nearest object)
    The tower select the nearest enemy (Enemy A) to be the shooting target and shoot a bullet towards Enemy A. Some of my towers aren’t shooting bullets but magic orbs, so I set their speed a bit low, so while the bullet is moving towards Enemy A, another ememy (Enemy B) appears to be the nearest enemy to the tower, and the tower now defines Target B as the target. Now, the bullet that is originally moving towards Enemy A will turn towards target B. The bullet will eventually be deleted because it always has a target to collide with.
    The code:
    Creating bullets


    Moving the bullets

  2. Version 2 (linking bullets and targetted Enemies)
    The tower select the nearest enemy (Enemy A) to be the shooting target and shoot a bullet towards Enemy A. While the bullet is moving towards Enemy A, another ememy (Enemy B) appears to be the nearest enemy to the tower, and the tower now defines Target B as the target. Now, the bullet will still move towards Enemy A. But the problem is that when the enemy died when the bullet is moving towards it, the bullet will stop halfway. I tried to delete the bullet by using take into account… but i don’t know why it won’t work.
    The code:
    Creating Bullets:


    Moving the bullets:

    Deleting the bullets that does not touch the targetted enemy when the targetted enemy died :

So in conclusion, how can I make the bullets meet those 3 conditions?

#2 and #3 seem incompatible or redundant. :thinking:

Gruk is right, something about your conditions is not adding up.

I would try to store the x- and y- position of the enemy that is nearest to the tower as object variables of the bullet. Then you use these variables to shoot the bullet to that position. When it arrives there you delete it (in case it did not hit any enemy on its way). So, not sure if that meets your conditions but it could be a start.

Sorry I think I did a mistake writting down the conditions. Editted the post.

Hmmm…I will try that

After thinking for a while, I realise that why not just stop and hide the enemy when it dies,

letting all the bullets that target it arrive its position then only delete it

But I am not quite sure how to code the quoted part:
Did what you mentioned and it works:

The part that I don’t know how to continue:

If you want the bullet to follow the enemy, then my suggestion won’t work.

I think the problem with this is that you deleted the object before the “take into account all”

Oh no wonder. I understand now. But I think even if this line of code works, it does not fulfill this condition

Actually what I wanted to ask is that, is there a way to check how many object(s) are linked to an object?

I don’t know if there’s a specific linked count expression (I don’t see one) but when you use the “take into acount all” as a condition it selects all of the linked objects. So, you could use the “PickedInstancesCount” expression.

2 Likes

Thanks for the hint, I think I made it by adding 1 to a variable while linking the obejcts and subtracting 1 after deleting the bullet so that the enemy will be deleted when no bullet is targetting it. I am not very sure whether the code theorically meets the conditions stated above but for my observation till now it meets all the condition:
Tower creating bullets:


Moving the bullets:

Delete bullet when it touches enemy:

Stop and hide enemies when their hp is 0 and delete them when no bullet is targetting them:

It looks good. One thing. You might need to move the “pick the nearest” condition to the bottom of your conditions. The conditions are checked in order. If it picks an object that doesn’t meet the last 2 conditions then it won’t create a bullet. Each condition narrows the group of picked objects. You only want to pick from valid ones.

Oh ok. Will do that. Thank you

1 Like