Follow an Object Along only 1 Axis

Hello everyone!

I was wondering if someone could explain to me how to make an object follow the motion of another object, but only along one axis. I know that the Steering actuator does a great job of basic following, but I couldn’t figure out if there was a way to restrict it to only follow the target object along a single axis.

You could use the constraint brick to force it to stay only on one axis, but it’s not the best solution.

A few lines of python will do the same, but allow much better control over what you want.

Are you using the same axis? Like object1 Y axis follow object2 Y axis? Or obj1 X axis follow obj2 Y axis


import bge

def followOnX():
   target = getOwner()
   source = getObjectToFollow()

   target.worldPosition.x = source.worldPosition.x

# This is just an example to read the source object from e.g. a trackToActuator
# You can retrieve the source object from wherever you want.
def getObjectToFollow():
   try:
      return getOwner()["_internal.source"]
   except KeyError:
      source = getActuatorObject()
      getOwner()["_internal.source"] = source
      return source

def getActuatorObject():
   for actuator in  bge.logic.getCurrentController().actuators:
      try:
         return actuator.object
      except AttributeError:
         pass

def getOwner():
    bge.logic.getCurrentController().owner


@Monster:

That method works perfectly! Thank you for your help!