How to have an object "track-to" a spawned object

This is something I’ve been stuck on before and probably asked before, but cannot for the life of me find any related questions online.

So I need a ways to have health bars track to the player, but still be able to have the player die and respawn with a new player. In otherwords, I need the track to object to always be the most recent player and not the player I have in a hidden layer.

I started to try piecing together a code, but I almost remember there being a simple script out there. This is what I started with, but I am going down the wrong track…
Any help or forward to another post is greatly appreciated! :slight_smile:

import bge

def main():
    cont = bge.logic.getCurrentController()
    own = cont.owner
    sens = cont.sensors['Always']
    actu = cont.actuators['Track']
player = obj.Controller
last = player.objectLastCreated
    if sens.positive:
        cont.activate(actu.last)
    else:
        cont.deactivate(actu)
main()


BIG EDIT:

I think this works for the healthbar assuming your player object is called ‘Player’ which can be spawned from an inactive layer, and an always sensor with true triggering executes this script (I’m also making a lot of other assumptions):


import bge

controller = bge.logic.getCurrentController()
own = controller.owner

scene = bge.logic.getCurrentScene()

# You must have an actuator called 'TrackActuator' for this line.
trackActuator = controller.actuators['TrackActuator']

if 'trackActuatorActive' not in own:
    own['trackActuatorActive'] = False

'''
Either the player is in the scene, or not.
If the player is in the scene, the player should
be in scene.objects. Otherwise, the player has 
been removed from the scene and is not 
available until respawn.
'''
if 'Player' in scene.objects:
    if not own['trackActuatorActive']:
        trackActuator.object = scene.objects['Player']
        controller.activate(trackActuator)
        own['trackActuatorActive'] = True
else:
    if own['trackActuatorActive']:
        controller.deactivate(trackActuator)
        own['trackActuatorActive'] = False

Also, you shouldn’t try to access objectLastCreated from a controller, because it doesn’t exist (it’s only for an add object actuator), and ‘obj’ doesn’t have a reference to anything in your code (nor does ‘Controller’). Finally, careful with your indentation please.

this works well :smiley:



import bge
from mathutils import Vector


def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner
    
        
    
    Target = own.scene.objects['Target']
    
    if 'Helper' not in own:
        own['Helper'] = own.scene.addObject('Helper',Target,0)
        own['Helper2'] = own.scene.addObject('Helper',Target,0)
    local = eval(own['local'])
    
    r = Vector(local).magnitude
    
    own['Desired']=str(local)
    targetPos1 = Target.worldPosition+(Target.worldOrientation*Vector(local))
    
    
    if 'p' not in own:
        own['p']= own.scene.addObject('point',Target,0)
        own['p'].worldPosition=targetPos1
        own['p'].setParent(Target,0,1)
        
    
    
    v2 = own.getVectTo(Target)
    
    
    v2t = Target.getVectTo(targetPos1)
    
    if v2[0]>r:
        #float to radius
        own.applyForce(v2[1]*15*v2[0],0)
        own.applyForce([0,0,own.mass*9.8],0)
        own.worldLinearVelocity*=.5
        
    else:
        own.applyForce([0,0,own.mass*9.8],0)
        own.worldLinearVelocity*=0
        if 'rotMe' not in own:
            if own.getDistanceTo(targetPos1)>.05:
                v2o = Target.getVectTo(own)
                own['Helper2'].alignAxisToVect(v2o[1],0,1)
                own['Helper2'].alignAxisToVect([0,0,1],2,1)
                local = own.worldPosition - Target.worldPosition
                own['local2'] = own['Helper2'].worldOrientation.inverted()*local
                own['x']=.0025
               
                own['rotMe']=True
        else:
            if own.getDistanceTo(targetPos1)>.5:
                v2t=own['Helper'].getVectTo(targetPos1)
                
                own['Helper'].alignAxisToVect(v2t[1],0,1)
                own['Helper'].alignAxisToVect([0,0,1],2,1)
                
                rot = own['Helper2'].worldOrientation.to_quaternion()
                rot2 = rot.slerp(own['Helper'].worldOrientation.to_quaternion(),own['x'])
                if own['x']<.999:
                    own['x']+=.0025
                own['Helper2'].worldOrientation=rot2
                p2 = own['Helper2'].worldTransform*own['local2']
                v2b = own.getVectTo(p2)
                own.applyForce(v2b[1]*250*v2b[0]*v2b[0],0)
                
                own.applyForce([0,0,own.mass*9.8],0)
                D = own.getDistanceTo(targetPos1)
                if D<.5:
                    own.applyForce(v2b[1]*2750,0)
                    own.worldLinearVelocity*=.55
                    own.color = [0,0,1,1]
                    own['D']=D
                    
                else:
                    own.applyForce(v2b[1]*1500,0)
                    own.worldLinearVelocity*=.55         
                own.alignAxisToVect(v2[1],0,1)
                own.alignAxisToVect([0,0,1],2,1)
                own.applyForce(v2b[1]*1750*v2b[0],0)    
                
            else:
                own.worldPosition=targetPos1
                own.alignAxisToVect(own.getVectTo(Target)[1],0,1)
                own.alignAxisToVect([0,0,1],2,1)
                own['Helper'].endObject()
                own['Helper2'].endObject()
                own.setParent(Target)
                own['p'].endObject()
                own.color = [1,0,0,1]
                own.state = 2
                
                   
                
                
            
        
                
                        
            
                    
                
                
        
        
    
    
    
    
    
            
        
                
    
    
main()



Attachments

FloatToRadius_And_Rotate_to_Place_6_b.blend (463 KB)

So I need a ways to have health bars track to the player, but still be able to have the player die and respawn with a new player. In otherwords, I need the track to object to always be the most recent player and not the player I have in a hidden layer.

easiest way to get healthbars pointing to the camera at all times, no matter where you look at without any scripts or bricks, will be the bilboard option in the materials setting.

@BluePrintRandom, Wow, that’s amazing! That will do what I need and then some. I was going to use something more rudimentary, but I think I will use this for the enemies moving toward the attack points for the player to defend in my game.

@Cotaks, completely forgot about the billboard option. That will work best for the health bars. Thank you :slight_smile:

for a better billboard if you like python and despise finicky material settings:

obj.worldOrientation = scene.active_camera.worldOrientation  #object always aligns to camera upright viewspace
obj.alignAxisToVect((0,0,1), 1, 1.0)  #OPTIONAL: snap to world Z up

the health bar needs to be modeled with Y up, and Z facing camera

For some healthbars/healthbar effects, a better option could be the ‘Halo’ Face Orientation in the Materials settings for objects (of course, there are other ways to orient a healthbar). Note that this orientation has some limits.

@Mirror|rirriM
Golly, those aren’t the most recent threads!

Ah, that’s true, but didn’t I reference some youtube videos/threads/resources that were old and
still seemed to help on other recent threads? That’s a good observation though.

take a look at this old test of mine, it got health bar that track to player
the health bar are on monsters but the idea should be the same.

http://15b.dk/blendfiles/spawner-test.blend