need a logical tip for AI

I want object 1 to take object 2 to object 3 as illustrated.

I’ve tried several methods but they weren’t practical because I want it to not depend on the objects positions, also object 2 might be moving.

I don’t want a code i just want the logical steps of doing such a thing, just something to put me on the right track.

Thanks in advance :slight_smile:

I was first going to suggest a finite state machine (FSM) attached to object 1 (your AI agent) as a good way to break up the behaviors (get object 2, go to object 3). But you really only need one “goto” behavior.

Let’s say you have a component called Goto that has a Target parameter. Attach it to object 1, and set the Target to object 2.

In Update(), Goto can compute a path to its target (object 2) and update its velocity. If you want to get fancy, you can examine the target’s velocity to compute an intercept course. How velocity translates to actual motion depends on how you’ve implemented motion. If you’re not doing animation, you can call LookAt() to face the target, and change the transform.position (= velocity * Time.deltaTime) in Update(). This is just a straight move to the target. You’ll need to write or use a path finder such as AngryAnt’s Path to move around obstacles.

When object 1 reaches object 2, it could trigger a collider on object 2. Object 2’s OnTriggerEnter() method would then:

  1. Attach itself to object 1.
  2. Change object 1’s Goto target to object 3 (i.e., go to object 3 now).

When object 1 reaches object 3, it could trigger a collider on object 3 that would:

  1. Remove object 2 from object 1.
  2. Change object 1’s Goto target to null (i.e., stay here).

So you really only have two components in this system:

  1. A Goto behavior, and
  2. An OnTriggerEnter() handler that updates the Goto target and attaches/detaches a child object.

Mmm what about,

float distance = (obj2.position - obj1.position).magnitude;
// You get the distance between the two and if not claose enough obj1 faces obj2 and then moves towards it
if(magnitude < range){
   obj1 looks at obj2
   obj1 moves forward
}else{
   //At this point we are close enough, we parent the obj2 to obj1 that is now "carrying it around"
   obj1 picks up/parent obj2
   obj1 has obj2   // here we change a bool to state that we have obj2 
}
// if obj2 is carried then we can apply same process towards obj3
if (obj1 has obj2){
   obj1 looks at obj3
   obj1 moves forward
   if(distance with obj3 < range )
      end of an exciting game
}