- Home /
Change AI Follow Players
I am making a script that will make the AI follow the player. If the player is caught, it will follow the second player. If the second player is caught, it will chase the third player. And so on.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 1;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
That's what I got so far.
Answer by SomeGuy22 · Sep 03, 2012 at 02:06 PM
var players = gameObject.FindGameObjectsWithTag("PlayerNotCaught");
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in players) {
if (go != null)
{
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
target = go.transform;
distance = curDistance;
}
}
}
Put that code in the Update() Function, and get rid target = go in Start(). Change the tags of your players according to if they're caught or not. Not caught will have the tag "PlayerNotCaught" so the AI can determine them from the rest of the players.
This script will choose the closest not caught player, but if you want to go in order of player 1, 2, 3, 4, etc., you will have to use an if statement according to the current player in the array. Something like, if (player[i] == 1) else if (player[i] == 2) then target = player.
I like the second part of your paragraph, that is the exact thing that I need. But how do you write that on a script. The (player[i] ==1) etc. Should I still include the var players or should I replace them with The (player[i] ==1) or something.
$$anonymous$$eep the var players (it's always good to hold a player list anyways) and have some sort of variable on the player script called something like "PlayerNumber" and then check if players[i].GetComponent(PlayerScript).playerNumber == 1
Specifically, you'd want to write a "for" line: http://answers.unity3d.com/questions/11547/how-do-i-use-for-to-create-loops-in-my-script-and.html
Should I make another script for the script that you just made? because That is in javascript.
Oh my bad, you'll have to translate that into C sharp. I think it's pretty close to what I wrote.
Also you'll need another script if you don't already have a player script. You'll want to keep of the player numbers with function OnNetworkInstantiate ()
Great, I am glad this helps ! Please feel free to thumbs up :)
Your answer
Follow this Question
Related Questions
How do I make an object follow the player? 1 Answer
AI Help FPS 0 Answers
Problems with simple AI script 3 Answers
Player moves faster when fps is higher? 1 Answer
How to make an AI like slender ? 1 Answer