Following AI - Similar to Snake Game

Hello all,

I am new to Unity (this is my first post) and I am (very sad to admit) quite poor with my math skills. As a programmer, that really isn’t a good thing. However, I have come here get some help. What I am trying to do is create a game with just spheres for now. The main player is a sphere, and your friends are spheres. When I “pick up” my friends, I want to them follow directly behind me, but rotate nicely when/if I change angle.

A few problems I have run into, setting the position to the position just behind me works out well enough, except when I change direction, it moves directly behind me which looks silly, also which I haven’t a clue in my mathless brain to fix that. I was told Translate isn’t great due to “jittery” movement, so I am going to try and avoid that. I have set up rigidbodies on each object so that I can move them via rigidbody transforms, but I haven’t really gotten very far. The most common problem I run into is that if I don’t just set the friends position directly behind me, and I say velocity +=, the friend eventually “lags” behind because it can’t really keep up with the players speed. I’ve worked on it for a few days and I looked at a few other scripts, and for following AI they work great, but just not for my example.

As additional info, they spheres do not collide with each other, ever, but I’m trying to make it follow directly behind, without passing through the player. Any help would be greatly appreciated, thank you again for reading. :slight_smile:

Lots of ways. One cute trick is to have them turn slowly to face the leader, and always move forward. Something like (this isn't as long as it looks):

Transform leader; // set when we're "picked up", to sphere we're behind

// In Update:
Vector3 toLeader = leader.position - transform.position;

// Spin to face our leader:
Quaternion wantDir = Quaternion.LookRotation( toLeader );
transform.rotation =
  Quaternion.RotateTowards(transform.rotation, wantDir, 60*Time.deltaTime);
// NOTE: the 60 means they turn at 60 degrees/second

// Move:
if(toLeader.sqrMagnitude > 2.0f*2.0f) { // stop when we get within 2
  rigidBody.velocity = transform.forward * 30;
  // NOTE: 30 is the speed in meters/sec.
  //   Unity automatically figures out correct amount per frame
}
else {
  rigidbody.velocity = Vector3.zero; // prevent coasting
  // NOTE: w/o this, it will coast to a stop, which could also look nice.
}

If you get the move speed and turn radius tweaked correctly, it can make some nice wide turns. If the turn is too small, you get a cool "spiral in" effect.

EDIT (reply to comment):

The angle changes smoothly since, instead of snapping to what we want, we MoveTowards to the goal. The same trick can be used to smoothly change velocity (untested):

Vecor3 wantSpeed;
if(toLeader.sqrMagnitude > 2.0f*2.0f) wantSpeed = transform.forwards * 30.0f;
else wantSpeed = Vector3.zero;
rigidbody.velocity =
  Vector3.MoveTowards(rigidbody.velocity, wantSpeed, 40.0f * Time.deltaTime)_;
  // NOTE: 40.0f is the accelaration in M/S. Can freely change it.

Hey Owen, thanks again.

I ended up writing it like this

Vector3 toLeader = leader.position - transform.position;
		
		//Spin to face leader
		Quaternion wantDir = Quaternion.LookRotation(toLeader);
		transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir, 360*Time.deltaTime);
		//Note: the 360 means they turn at 360Deg/s
		
		transform.position = leader.transform.position - (toLeader.normalized * 1.5f);

that worked really well for what I was doing.

Thanks again!