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.
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).
The bullet must hit the Enemy A whenever Enemy A goes, except Enemy A died before the bullet touches it.
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:
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
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:
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.
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.
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:
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.