targeting system

Hey i was wondering how to make a targeting system like the won on sarge’s heroes were the gun automatically locks on to the nearest enemy.

So you want to get the nearest object and track to it?

Here is a blend:
You would attach whatever logic is on the arrow to whatever adds objects in your game.


Tracking Arrow with Scripts.blend (156 KB)

thank you so much i will be sure to put you in the credits

ive been looking for a way to do this as well :smiley:

my thanks too… :yes:

Hey marwin, thanks for the reply. The blend isn’t exactly the whole story. Its really basic.

Here is a getClosest function. It has lots of different variables and such that help get the closest object.

##Gets nearest object (replaces near sensor) If you just want to get the nearest object leave prop as None##
## obj = the object to start from. prop = prop objects must have to get checked. maxdist = maximum distance to look.
## dynamicOnly = get dynamic objects only. oblist = the object list to look through. (leave as None to look through all objects.)
## ignore = a list of objects and things to ignore.
##Note: Script will not return the object defined as obj.
def getClosest(obj,prop=None,maxdist=None,dynamicOnly=0,oblist=None,ignore=set()):
	closest = None
	closest_dist = maxdist
	objects = oblist or GameLogic.getCurrentScene().objects
	ignore = set(ignore)
	for ob in objects:
		if (not prop or prop in ob) and (not dynamicOnly or ob.mass > 0) and ob not in ignore:
			dist = obj.getDistanceTo(ob)
			if closest_dist is None or dist < closest_dist:
				closest = ob
				closest_dist = dist

Here is a track function that tracks.

#Tracks to object, no actuators needed.
#axis: (default=1)#0=x #1=y #2=z | Note: use negitive numbers for minus axis.
#factor (float) - Only rotate a fraction of the distance to the target vector (0.0 - 1.0)
#ob can be object or list.
#use3D is the same as the 3D button on the actuator.
def track(target,ob,axis=1,factor=1,use3D=1):
	vec = target.getVectTo(ob)[1]
	if axis < 0:
		vec = listTimesNum(vec,-1)
		axis = axis*-1
	final = [0,0,0]
	if not use3D:
		final[0],final[1] = vec[0],vec[1]
		final = vec
	target.alignAxisToVect(final, axis, factor)

NOTE: These scripts are for advanced usage only.

oh i see:eek: so we have this function in the api???
didnt see this there :o


to get the nearest object leave prop as None##

funny…didn’t figured this out ^^

i hope i am not asking o much is there a way to make it where it only track’s to if you are looking at the enemy or is there a way to limit the rotation.

These functions were written by me. They are not in the api.

EDIT: If you want to find these functions and updates on them. Just check out the scripts section on my website. Its all part of the BANANA all purpose programming library.

Thats more complicated… Do you know any python? I just need to know whether I should add instructions to the blend.

I have all the scripts prepared…I just need to do some stuff to make it more user friendly.

not really you probably should make in user friendly i just started learning python but i couldn’t read a script if my life depended on it.

Okay. I will work on a blend. I am going to comment it all. (Mostly) Which means that you should read the lines with # signs.

Also, I will be making it property based. So technically, you won’t even have to look at the script to edit it.

k thank you

Okay. It didn’t work out perfectly. (At all.) but it does work. And once you actually set it up in your game, it will do wonders.

TrackToClosestInRange.blend (298 KB)

Great it works amazing it took a bit to get it set up now is there a way to rest the position of the object to where it was before the script edited it.

Yes. You just have to reset its orientation…

Again…an odd concept to grasp but not too hard.

Dude thanks so much

thanks … just beggining to understand python as well ^^

These functions were written by me. They are not in the api.

thanks for sharing :smiley:

OK i have run into a problem is there a way in your script to change a bool properties because i need it to reset it’s orientation (witch i figured out how to do) when the gun does not sense a target. so like the code that senses the target could you put if not reset orientation or change bool to true.

Okay. So I updated the code and made a few major changes.

  1. Made it so that if there is no target, it will reset orientation. (Not instantly, smoothly.)
  2. Added a property called “On” that dictates whether or not the gun thing is on.
  3. Made it so that if gun thing is off (On property is False) the gun will reset orientation (again, smooth)

Unfortunately I am quite busy so I didn’t comment any of the additions I made. Don’t worry though, it is all pretty simple and there is no really big addition.

Make sure you ask if you need anymore help!


TrackToClosestInRange.blend (300 KB)

Um… i opened the file then i pushed p and it did not track to anything i then turned the factor up and still it did nothing.