Hi ! Question: How do I make that unit continue shooting until it destroys enemy? Currently, when an enemy appears (e.g. a tank that is closer to a mech unit, closer than a drone), then the mech stops shooting at the drone, turns around and fires at the tank.
Tank and drone are in the group “all player units”
This is one way of doing it. It uses an object boolean variable for the object that is locked onto a target and links the object to its target.
For my test project, there were multiple tanks and mechs. If objects of the same type could shoot at each other then a different approach would be needed because you can’t link instances of the same object. Meaning you can’t like a tank to a tank.
If that was the case then I guess you could use object IDs instead of linking or maybe some other method.
@Keith_1357 Works, but only one problem, all units have a condition assigned, e.g. mech distance to tank and fire action, so if there are 2 or more units at such a distance then the mech shoots at all at once, how to make the units shoot only at one target at a distance of 300 px (for mech). I have 2 groups in the game: player - i.e. all player units (e.g. tank) and enemies (e.g. mech). From these groups, the units have different shooting distance and movement speed. Is it possible to use groups instead of single units? I ask because there will be a lot of different units in the game.
Any object or object group can be used with the for each object. You can add a distance condition and if they have different ranges then you could use an object variable instead of a fixed number.
Say you had 2 groups, enemies and enemyTargets
For each enemy
Dist to enemyTarget < enemy.Range
Pick nearest enemyTarget
You could then change the locked boolean and link the objects
The pick nearest object would limit it to 1 target unless you’re talking about limiting each target to only 1 attacker.
If so, you could add a boolean to the target object as well.
Enemy object variable LockedOnTarget is false
TargetObject variable Targeted is false
Distance check
Pick nearest TargetObject
Set LockedOnTarget to true
Set Targeted to true
Link enemy to enemyTarget
You can simplify a lot of things by uskng object variables. You could use a team ID and target a specific Team or any team that isn’t the current team.
You can use the object name or a variable for the type of vehicle. You could add a target type variable to say the tanks that lets you automatically filter just the target type.
Instead of seperate events, you can usually use conditions to reuse the same events for different scenarios.
Im not clear on what you meant or what you’re doing. I might be way off base. If so, we’ll need more info to help you.
Thanks for the answer, I did as in your picture and it works, but the problem is like in my picture, the mech shoots at 2 units at the same time, to the cannon and the tank . (gold bullets is from the mech)
The same goes for all other units, e.g. tank (all units have the distance condition (to other units, e.g. mech air cannon2 - fire action) (yelow bellets is from the tank
so each unit can shoot at several units at the same time, probably because of this conditions and the fire actions.
Is it supposed to shoot at multiple targets? My example use a boolean to prevent a tank from picking a new target until the previous one is killed.
Can you post a screenshot of the relevant events?
@Keith_1357 Your example is perfect and thanks for that, my problem also occurred before your help, the difference was that the bullets flew only at one unit even though they killed 2 or more, now they fly simultaneously at 2 or more targets in all directions, but that’s not important, your method helped me of course, but I would still like to eliminate this problem, namely I would like the units to shoot only at one target. For example, I have conditions and actions for the mech: I have conditions and actions for each units from the all player units group separately, i.e. the condition that the mech at a distance of the tank / cannon / drone at a distance of 300 px shoots, i.e. if all these units are at a distance of 300 px from the mech, the mech shoots at all of them simultaneously. I’m new to gdevelop and that’s probably why I add these conditions and actions separately to all units, it’s possible that there’s another way and it’s possible that it would solve the problem, but if there’s another way that’s also good because anyway I already have a lot of units and consequently a lot of added conditions and actions and if it doesn’t need to be changed or deleted, and it’s enough to add one or two conditions that cause the unit to shoot only one unit then I’d like to know this solution. Once again , I would like the units to shoot only at one target.
You would need to repeat the same strategy for all of the different types of vehicles. You might be able. To simify it through object variables. Unfortunately, you can’t filter objects using a condition using the object name. You could create a variable for the vehicle type. You could then use a condition to filter for just that object type or types. I don’t know if each vehicle type can only shoot at 1 type of vehicle or multiple types.
You could use the troop type of the target in combination with another variable for the target type used by the object that is shooting. You could then go through a group of different troops and use the target type to target the right object type.
It can get complicated if objects of the same type target each other.
This all might not fit your project and you may not understand what I’m referring to. I would create a short example project but I really don’t have the time.
For, now unless you understand what I’m saying or someone else has a better idea, you could try to modify the events for the other troop types to work like my example.
@Keith_1357 In the scene I have a lot of units, look in the screenshots, for example for tank1x3 I had to add such conditions and actions with a distance of 300 px and fire to every other unit (from the enemies group) (the distance will change later for different units) and the problem is that tank1x3 performs conditions and actions even though it is facing only one unit, and shoots at everyone around, because that’s what it has in the condition, i.e. 300 px to every different unit within. I have no idea how to make it shoot only at one.
The easiest way to handle multiple similar objects is to put them into a group and use for each object.
I’m not clear as to the best way to handle your situation and I honestly don’t have the time required to dig deeper.
If you use object variables for your objects then within the for each object using the group name you could use an object variable like ShootingDistance instead of a fixed number. The same approach can be used for other things.
For each troop
… Distance between troop and target is less than troop.ShootingDistance
…pick nearest target
… Link and other things
The issue would be choosing the target. That would be more complex but I’m sure there’s a method that can be used that could keep it simpler than using seperate events for each object type.