I’m doing tower defense game, and I have player spawner (when i click on it the player appears), and I have enemy spawner that spawns enemies constantly. The player, when spawned (better said, bought) suppose to track the enemy. I have three layers - level, player and enemy. The main problem is the player won’t follow the enemy, he just “looks” at one point and I realized it’s the spot where is enemy on the enemy layer. How to fix this issue?
use addobject, to add the enemies using python
after each enemy is added, add it’s key to a list
Enemy = own.addObject(properties)
EnemyList = EnemyList + Enemy
then save the property.
own[‘EnemyList’]=EnemyList
Then in each enemy have code to look it’s self up and remove itself from the list if it dies,
Then in player do
always copy list
then for items in list:
(tab)D = own.getDistanceTo(enemy)
(tab)if D < own[‘ClosestD’]:
(tab)(tab)own[‘Target’]=enemy
edit-(the player item could actually add the items to the scene, using the spawner as a reference object)
(the point of this code is to not use scene.objects[‘Name’] etc so that you don’t have a huge lag when there is lots of items in the scene)
I agree. Even if it is a HardCore answer to a ‘not so simple’ question… You can also use the spawn.instantAddObject() function.
Then spawn.objectLastCreated.name to individualize each ennemi…
Thanks guys, but I don’t know anything about Python and I’m using 2.49… I’ll take a look at those answers, thank you very much for replies.
Why not switch to 2.69?
I can provide some examples of logic+python.
Attachments
ActivateAndGrabAndMove.blend (563 KB)SignFlash.blend (468 KB)3AnimationsPropertyControled.blend (458 KB)
I’m going to lose lots of time learning the new version because it’s so complex… also I don’t know Python. But if it’s only choice…
Switching to 2.66 is not so hard.
When 2.5 came out, i sticked with 2.49 for some time, until i realized that 2.5 wasn’t so complex. Take your time to learn about it
What is happening there with your problem, is that most likely you’re using the edit object actuator set to Track To. The Track To acutator will only look for objects with the name specified by you, however it will ONLY look for that object. Since you have many enemy objects, it would be better if you used python to look for a property on each enemy object, and make the player track to the closest one, but since you’re using 2.49 and don’t know a lot of python, i guess i can’t help you so much :S
It doesn’t matter, I did it using this:
cont = GameLogic.getCurrentController()
own = cont.getOwner()
scn = GameLogic.getCurrentScene()
ob = scn.getObjectList()
track_ob = cont.getActuator(“track”)
def trackToClosest(cont):
own = cont.owner
prop = ‘enemy’
obl = [ob for ob in scn.getObjectList() if ob.has_key(prop)]
closest = [obl[0], own.getDistanceTo(obl[0])]
for ob in obl[1:]:
dis = own.getDistanceTo(ob)
if dis < closest[1]:
closest = [ob, dis]
track_ob.setObject(closest[0])
GameLogic.addActiveActuator(track_ob, 1)
trackToClosest(cont)
The problem is that virtually no-one here knows 2.49 python any more, so if you want coding help, you will probably have to switch.
If I understand correctly. not sure I do;
I don’t see a motion actuator in your script. So the player only looks at the enemy, if you add a motion actuator, then he should move towards your enemy.
Here’s the track to closest script I always use. It works great. Just modify it to suit your needs. Thanks go to Siegel for sharing his script.
#################################################
# 2.49 #
# "Track to closest object" with movement script by Siegel #
# #
#################################################
# The object will point and move toward the closest object with
# the property "cube".
#add a Near sensor called "near" with property: Cube,(or whatever just remember to change it in the script) Dist 100.00 reset 101.00
#You can shorten this distance.
#True pulse checked
#add Python script
#add a Trackto actuator called "trackto" OB: leave blank, Time: 10
#add a Motion actuator named "walk" Y loc: 0.03 L
# add a Cube with a property of "Cube" Float: 0.00 Static or Dynamic, for enemy to track to. animate the cube. Or whatever.
import GameLogic
controller = GameLogic.getCurrentController()
owner = controller.getOwner()
#sensors
near = controller.getSensor('near')
#actuators
trackTo = controller.getActuator('trackTo')
walk = controller.getActuator('walk')
def dist(a, b):
"""
Calculates the distance between two 3D points.
Requires 2 arguments namely 2 3D position lists.
"""
return ((abs(a[0] - b[0]))**2+(abs(a[1] - b[1]))**2+(abs(a[2] - b[2]))**2)**0.5
objectList = near.getHitObjectList()
if objectList != None:
closestDistance = 100
closestObject = None
for object in objectList:
position1 = owner.getPosition()
position2 = object.getPosition()
distance = dist(position1, position2)
if distance < closestDistance:
closestDistance = distance
closestObject = object
if closestObject != None:
trackTo.setObject(closestObject)
GameLogic.addActiveActuator(trackTo, 1)
GameLogic.addActiveActuator(walk, 1)