FPS Homing shots?

Blitz3D Forums/Blitz3D Beginners Area/FPS Homing shots?

Robey(Posted 2010) [#1]
Okay, I figured out how to make enemy's shots home in on the player using "pointentity" . That's easy enough, seeing as there's only one target the shots can point at.

However my question is: how is the reverse possible? i.e. Making the player's shot (homing missile launcher or what have you) home in on the closest enemy. How can I make the player's shot distinguish between one enemy, and the rest of them?


BLaBZ(Posted 2010) [#2]
I typically give each of my enemy type's a unique "ID," every time you make a new enemy make the ID = ID + 1

here's some code I wrote awhile ago the finds the closest enemy, in this case "aiunit" is the unit finding the closest enemy unit



closestenemy_distance# = thelevel\groundwidth + thelevel\grounddepth;;how far unit will search for enemy
			closestenemy_ID = 0
			For enemy.unit = Each unit;;find the nearest enemy
				distance# = EntityDistance#(aiunit\mesh,enemy\mesh)
				If distance# < closestenemy_distance# And aiunit\ID <> enemy\ID And enemy\team <> aiunit\team;;don't include itself or its team to find the closest unit
					closestenemy_distance# = distance#
					closestenemy_ID = enemy\ID
				EndIf
			Next


It makes sure that the enemy unit is not on the same team and doesn't contain the same id as the unit checking searching for the nearest enemy


PowerPC603(Posted 2010) [#3]
If each enemy is represented by a type, and also your bullets, then it's very easy to do.

Add one field to your bullet, called Target.Enemy.

As soon as you press the fire-button, you search through your enemies to find the closest one (you can determine that by EntityDistance if needed).
Store that enemy-type in your Target.Enemy field, so the bullet always knows which enemy to track.

Then, in your UpdateBullets function, always let that bullet point towards the enemy stored in Target.Enemy.


_PJ_(Posted 2010) [#4]
Or you could use the handle of the type instance of that particular target.