So I used an Edit Object Track-to actuator to lock onto an enemy,
But in the game logic, I can only select one object.
How do I make my character lock onto the nearest enemy?
And then after that enemy’s defeated, how do I make my character lock onto the next nearest enemy?
The only thing I can think of is to have an Edit Object track-to actuator for each and every single enemy.
Each activated when each enemy is near.
But there must be a more efficient way for a game that has lots of enemies.
The responsibility of the TrackToActuator is to track a single target object. It does not care finding the target object (unless you set the target as name via python).
Imagine how many options there are to do so. The tracking will still be the same.
Hundreds of trackToActuators will not help as you still do not know which one to activate.
There is nothing build-in that provides you the closest enemy (unless there is only one). You will need Python to do the distance checks.
I suggest you look for threads dealing with “closest object”.
you can change the track to object in python…I do not see a way to do it without some scripting…
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
obj = scene.objects
someobject = obj['someobject']
track_to = own.actuators['Track to']
if own.getDistanceTo(someobject) < 3:#if my distance to the object is less than 3 units(meters for me)
track_to.object = someobject
else:
track_to.object = None#or a different object etc
Monster is correct though…you need some way to ‘point’ to that object…you can build a list that stores the objects that are within range of the player…
and assign a key to ‘tab’ through the objects in the list…
I recommend you investigate the use of python(I’m still learning python myself ;))
specifically how to use lists(read, write, index)
using ‘for’ loops
and looking for objects with a specific property inside the ‘for loop’…these are actually all trivial things to do, but if you are very new…your best bet is to go to youtube and watch some BGE tutorals or google ‘BGE’ + for loop, or the other things I mentioned.
Try to lean a bit on your own…if you get stuck…comeback, more than likely if people see you are legitimately trying they will help you out
as for testing the distance of one object to another…it is a simple line of code as seen above in my example.
I realize this is all a bit off subject from your question…but it is meant to guide you and not lead you down the wrong path…bricks are fine for jumping games or very simple things but, as 3D Upgrade said…you cannot make an rpg with bricks…you would go insane trying.
Well, if you really want to use logic bricks, you can actually! But it’s extremely limited and it wouldn’t be flexible.
You could setup a Near sensor that detects one thing you might want to lock-on to, and a button to activate it. When the sensors are true, you could then switch to a different state where the player is tracking to the same object the near sensor detected (this would be manually done). In this state switch back to the previous state if the button for lock-on is released.
Yes, you’d need to create a separate state for every singe instance of an object. Yes, it would use up a lot of states. Yes, the maximum amount of things to lock-on to would be 29. You also wouldn’t be able to lock-on to the nearest thing, you’d lock-on to the first object one of the near sensors activates on.
If you want to stick with logic bricks, try simplifying your game - otherwise you will need to learn some python (there’s not much python required to do this simply).
import bge
#get access to bge commands
cont = bge.logic.getCurrentController()
# this is the python controller calling the script
own = cont.owner
#this is the gameobject containing the controller running the script
NEAR = cont.sensors['NEAR']
#this is the near sensor connected to the controller
near = [ ]
#we create a empty list
Target = None
#we set a property called Target to None
Length = len(NEAR.hitObjectList)
#we see how many objects are inside the near sensor
#if only 1 object look at that
if Length==1:
Target = NEAR.hitObject
#if more than 1 object sort a list of objects, and get the closest
elif Length>1:
for ob in NEAR.hitObjectList:
# we build a list of tuples - small lists
near.append([ob, own.getDistTo(ob)])
near = sorted(near, key=lambda x: x[1])
#here we sort the list by the second value in each tuple
#this is advanced python, so read up on it some time it is handy
#this is the sub list with the lowest distance
smallList = near[0]
#the first value of the sublist is the game object
Target = smallList[0]
#if Target is not None
if Target:
#instead of using track to actuator we can just do the alignment in python
V2 = own.getVectTo(Target)
#Get vect to returns a list
distTo = V2[0]
angleTo = V2[1]
#this is the angle and distancec to a vector or object
own.alignAxisToVect(V2[1], 0,.1)
#this command aligns the axis of a object to a angle over time
own.alignAxisToVect([0,0,1],2,1)
#this keeps Z axis pointing up, one could also use a normal of a surface
#one could also use a local axis of a parent to make a turret etc
Thanks, BluePrintRandom!
By looking at your comments, i just realized cont means controller!!!
This really helps as a starting point,
because it’s more motivating to look things up when I can see the results,
and comments explaining things.
Thanks!