# Object Chasing Another Object

I have two objects, Object A and Object B. Object B can be constantly moving whatever I set its position to be, or it's movement. It could also not be moving at all. I want to be able to make it so that Object A chases Object B (by moving towards it every frame) at a constant speed where if Object B changes position, Object A's direction of movement changes as well. I am using normal Blender Render, so can someone suggest me an easy way to do this, even if it involves Python coding. It's basically making an object face another and translating on its own local forward.

Want I don't want is the two objects two have their vertex positions linked to each other so if I drag one in the editor, the other one will move as well (aka parenting). I do not want that.

This means that if Object A's speed is set to 0, then changing Object B's position won't affect Object A's position.

Again I want that so as I play the animation frame by frame, the first object is moving towards the second object at a constant speed and the second object itself can move somewhere else which will change the heading of the first object in order to still be following the second object.

• Have you tried setting your objects to follow a curve as opposed to parenting A to B? – Timaroberts Apr 19 '16 at 4:32
• I did think about that but it means I have to figure overly complicated math equations and it gets even hard when object b moves conplicatedly. – Bradman175 Apr 19 '16 at 4:33

Here is something I put together to answer this q about a month ago, noticed it active and dug it up.

Before running script set up a scene with a sphere named "Sphere" and a cone named "Cone", give the cone a track to constraint to make it face the sphere. The Cone is given a speed custom property. Animate the sphere and the cone will track it. This is done with frame change handlers.

NB: Need to fix for when the speed is zero, the track to constraint has no effect, (simple driver) 1 if speed > 0.0 else 0 and what to do when the objects collide.

import bpy
context = bpy.context
scene = context.scene

obA = scene.objects.get("Cone")
obA["speed"] = 0.01

obB = scene.objects.get("Sphere")
preloc = obB.matrix_world.to_translation()

def getloc(scene):
global preloc
preloc = obB.matrix_world.to_translation()

def setloc(scene):
global preloc

# set location for frame 1
if scene.frame_current == 1:
obA.location = (10, 10, 10)

loc = obB.matrix_world.to_translation()
v = loc - preloc
if v.length > 0.000001: # it has moved
print(scene.frame_current, v)

# move object A towards B
dv = loc - obA.location

speed = obA["speed"]
print(dv.length)
if speed > 0.000001 and dv.length > 0.000001:
obA.location += speed * dv.normalized()

bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_pre.append(getloc)
bpy.app.handlers.frame_change_post.clear()
bpy.app.handlers.frame_change_post.append(setloc)

• This is the kind of answer I want. I will check it out when I get home. – Bradman175 May 20 '16 at 0:28
• I'm still not home yet... – Bradman175 Aug 15 '16 at 5:32