Ai follow

hey how would i script and ai npc or ally to follow me when i move a certaint distance from them and play a walk or run animation depending on if i’m running or walking? i’d like to add this to about 3 characters.

use states. I’m working on one now for our game project but I haven’t had time to work on it.

Example: enum States{FOLLOW, STOP, LEAVE}

Next, define a wanted distance. you can check it using the Vector3.Distance(ai.position, player.position) function. When the player reaches a distance greater than this, the ai enters FOLLOW state and follows the player as long as the input keys are being pressed. If the input keys are no longer pressed and the ai is at the wanted distance, it enters STOP state which keeps monitoring the distance in order to follow when necessary. This state will only change if the player has moved to a greater distance than the wanted one. So you can turn and face the player in STOP state without worrying about anything. Also remember to rotate to the the direction the player is facing only when he is moving using: NPC.rotation = Quaternion.Slerp(NPC.rotation, Quaternion.LookRotation(player.position - NPC.position), turn_speed * Time.deltaTime);

I’m using direct translation of the AI at the moment. I’ll try changing it to use the NPC’s CharacterController.Move() when I work on it.

The LEAVE state is for when the AI happens to be in your way. I created a child object and a trigger in my NPC so when the player enters the trigger, the AI moves a little distance from the player. I do this incrementing the X and Y vectors by 2.

EDIT: Turns out all you have to do is put the target position vector(the position of the player - minus the distance the NPC keeps from the character) in the CharacterController.Move function.

var targetObj: Transform;
var speed= 10;
function Update() {
if (!GameObject.FindWithTag("cubao").GetComponent("MovieCharacter").pauseGame)
		return;
	var heading = targetObj.position - transform.position;
	var distance = heading.magnitude;
	
	if (distance > 1.2) {
		transform.LookAt(targetObj);
		var direction = heading / distance;
		transform.Translate(direction * speed * Time.deltaTime);
	}
}

thanks a bunch much appreciated

hey i’d like to share this with you guys on what i have so far i learned this from an old unity tutorial video i can’t seem to have my ally stop when they get close to me. here is the code. i started to realize when i was adding different status i’m making a baby finite state machine. kinda cool i’ve been working with unity for about a month some suff is hard, but i’m learning. my goal is to have fully functional ally’s and enemies and npc’s.

ar awareDistance:float = 15.0;
enum AIStatus {Idle = 0, Track = 1, Shoot = 2, Reload = 3, Cover = 4, Heal = 5, ChangeWep = 6, HealPlayer = 7,}
private var status = AIStatus.Idle;
var trackDistance:float = 10.0;
var player : Transform;
var runSpeed:float = 8.0;

var controller : CharacterController;
private var moveDirection = Vector3.zero;

function Awake()
{
controller = GetComponent(CharacterController);
}

function Update()
{
CheckStatus();

switch(status)
{
case AIStatus.Idle:
Idle();
break;
case AIStatus.Track:
RunToPlayer();
break;
}
}

function CheckStatus()
{
var dist = (player.position - transform.position).magnitude;

if(dist < trackDistance)
{
status = AIStatus.Track;

print (“I’ve found you but can’t stop running when I get there lol !!!”);
}

else if(dist < awareDistance)
{
status = AIStatus.Idle;

print (“I’m Aware Your In My Area…”);
}
}

function RunToPlayer()
{
transform.LookAt(player.transform);
moveDirection = Vector3(0, 0, 4);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= runSpeed;
controller.SimpleMove(moveDirection * Time.deltaTime);
animation.CrossFade (“run”);
}

function Idle()
{
animation.CrossFade (“idle”);
}animation.CrossFade (“idle”);
}

I think the reason the NPC is not stopping is because of the way you defined the moveDirection vector. You should use the player’s z position as the z value in the moveDirection vector.

If you use dist = player.position - transform.position
then you direction vector becomes

dist.y = 0; //<- so the NPC won’t fly or something
movedirection(dist.x, dist.y, dist.z);

oh man lol i gotta get better at these states.