Problem with track to nearest property with planetary gravity

could someone modify this to work with goran’s gravity script.Track to nearest property

put a demo.blend here containing both scripts

here is the blend.
pasteall.org/blend/index.php?id=50212
press w to walk around.

can’t move at all getting a few errors

Warning! user defined property name "debug" is also a python attribute for object "Camera"
        Use ob["debug"] syntax to avoid conflict
Warning, sensor "Always" has lost a link to a controller (link 1 of 1) from object "metarig.001"
        possible causes are partially appended objects or an error reading the file,logic may be incorrect
Warning, sensor "Keyboard" has lost a link to a controller (link 1 of 1) from object "metarig.001"
        possible causes are partially appended objects or an error reading the file,logic may be incorrect
Warning, sensor "Keyboard.001" has lost a link to a controller (link 1 of 1) from object "metarig.001"
        possible causes are partially appended objects or an error reading the file,logic may be incorrect

i am using upbge.it works for me.

i meant pressing w works for me.

Ok downloaded upbge, my script works gorans as well.

Your problem is that the cube is GHOST so uncheck that, and it fall down to ground again.

the problem is with my female npc when it is uses your track to nearest property
script.she will either fall forwards or fall backwards when following me.

Remove my script, as i said before you don’t need it. Now do the following:

  • select female box, set the trackTo to the player. (instead of script this way is way better)
  • select the player box, turn of ghost.

Now your problem lies in goran’s script, The script works perfectly but the part that is causing your issue is this line


player.alignAxisToVect(up_vec, 2, 1)

The script aligns the objects to it’s surface, so yes characters going to walk on/in/at an angle due to your world sphere. You can remove the line to see that the objects stay straight.

Or instead of aligning to the floor align z up by doing this instead


player.alignAxisToVect([0.0.2], 2, 1)

But then you can’t walk around the world i think.

i did what said before but it did not work.the whole point is for the npc to walk around on the
sphere with my character and to track to nearest thing with a particular property.are you telling me that is not possible without the glitch I am having.

Think so, because it does the same with the trackto brick without my script attached.
if i uncomment that line in gorans script the follower act like it should, but then it will not align to the world object anymore.

So without looking to it, i guess the upvector has multiple axis data instead of just altering the z axis only.

didn’t look at your blend. Can’t DL ATM.

I use alignAxisToVect instead of a track to script, so it should work.
first I get the scene object that I want it to track to.(player)

sce = bge.logic.getCurrentScene()
player = sce.objects[‘player’]

Then

vect = obj.getVectTo(player)[1] # if property is in player whatever. can’t remember exactly how to do this.
Then
obj.alignAxisToVect(vect, 1, .03) # enemy is the obj, tracks along the +Y axis
obj.applyMovement((0,.03,0),True) # move along +y axis

something like this. I’m not at my gaming PC ATM. So this is from memory.

So my enemy will track to the player up hills down hills whatever.

Hope this helps

i tried but the modified script of cotax spits out a error.
here it is.

# always -> python-module-track_to_closest.track-> edit object - track_to
# and a property "track_distance" on the object using this script
# and place a property on the objects you want to track to: called track_to


def track(cont):
    
    own         = cont.owner
    scene       = own.scene    
    track_to    = cont.actuators['track_to']
    
    ai = [obj for obj in scene.objects if 'track_to' in obj]
    
    if ai:
        
        closest_ai      = sorted(ai, key=lambda enemy: own.getDistanceTo(enemy))[0]    
        distance        = own.getDistanceTo(closest_ai) 
        max_distance    = own['track_distance'] 
        closest_ai.alignAxisToVect(vect, 1, .03)

        closest_ai.applyMovement((0,.03,0),True)



        
        if distance <= max_distance:

            track_to.object = closest_ai
            cont.activate(track_to)
        
        else:
            cont.deactivate(track_to)

as i said before get rid of the script. this is all you need

def track(cont):
    
    own     = cont.owner
    player  = own.scene.objects['PLAYER']
    
    axis        = 1 
    speed       = 1.0
    walk_speed  = 1.4
    
    own.alignAxisToVect(player.worldPosition, axis, speed)
    own.applyMovement((0,walk_speed,0),True)

but that does not track to nearest property.that is what I want with planetary gravity.


# always -> python-module-track_to_closest.track-> edit object - track_to
# and a property "track_distance" on the object using this script
# and place a property on the objects you want to track to: called track_to


def track(cont):
    
    own         = cont.owner
    scene       = own.scene    
    
    ai = [obj for obj in scene.objects if 'track_to' in obj]
    
    if ai:
        
        closest_ai      = sorted(ai, key=lambda enemy: own.getDistanceTo(enemy))[0]    
        distance        = own.getDistanceTo(closest_ai) 
        max_distance    = own['track_distance'] 

        if distance <= max_distance:

            own.alignAxisToVect(closest_ai.worldPosition, 1, .03)
            own.applyMovement((0,.03,0),True)

and remove the trackto brick

okay lots better.

i was a lot hasty.that did not work at all.i did what you said.did you test it with the planetary gravity
script.

you need to align z axis (2) to the planet center second in the script. order is important.

vref = planet.getVectTo(own)[1] # Global Vector Return
own.alignAxisToVect(vref, 2, 1.0)

https://docs.blender.org/api/2.79/bge.types.KX_GameObject.html#bge.types.KX_GameObject.getVectTo

NOTE: untested, im not at my PC.

@Lostscience
I assume that that would not work, goran’s scripts already aligns the objects to it’s surface, that aligning interferes with this script, as i said before. He either needs to write a new script or adjust it to work.

But on the other hand if it cancels out the other alignment it could work. my guess, it will not work, simply because both scripts are working on it’s own, it’s just that he needs to combine the 2 scripts into 1 in order to work.

The problem here is 2 alignments trying to align the same object(s) every frame