- Home /
Friendly AI that follows player and shoot zombies.
Sir/Madam I want such type of friendly AI script which would follow the player and shoot zombies just like left 4 dead 2. I am not that great in scripting (just started) so could you help me please.
Answer by shadowpuppet · Dec 11, 2017 at 05:49 AM
I made the very script for a robot helper I have. I basically just took the enemy attack script (what the enemy uses to attack me) and put it on my robot changing the target to gameObjectsWithTag == enemy. I put an an adaptation of the attack script on the robot ,as well, where I am the target - only his "attack" is just an idle animation which he does when he is within 2 units of me. and when I move...he "chases" me just as in the attack scripts. So I have an Attack script and an Ally script, and I just toggle between them depending on the conditions . In the attack script, if no enemies are within the maxDist range ( say 20 units) then I disable the Attack script and enable the Ally script so he will come to me and follow me. In the Ally script I do the same in reverse, if, while he is being my pal and following me around, an enemy comes within his maxDist range, I toggle off the Ally script and enable the Attack script. I tried putting them both in one script but his attack script got a bit hairy when I coded in seeking out the closet enemy to attack. So if you even have an attack script to start with then just modify them. Below is an old more raw version of the basic attack script I started with before making all the modifications( found it on line years ago) and it still has commented out explanations
using UnityEngine;
using System.Collections;
public class enemyAttack : MonoBehaviour {
public enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4};
public AnimationClip enemyIdle;//setting animations
public AnimationClip enemyWalk;//walking
public AnimationClip enemyRun;//running/chasing
public AnimationClip attack;//attacking player
public AnimationClip enemyKill;//killing player
public AnimationClip enemyDie;//killing player
public enemyHealth enemyHealth;
public Transform Player;//adds the player to the script
MonoBehaviour shootbullet;
private int Stopped = 0;
public int WalkingSpeed = 1;//sets the normal walking speed
public int RunningSpeed = 5;//sets the running (chase player) speed
public int DefaultRunningSpeed = 5;
public int MaxDist= 8;//maximum distance the player can be before enemy stops chasing the player
public float MinDist= 1;//how close the player can be to enemy for enemy to start chasing player
public EnemyState currentState;
Animation anim;
private bool PlayerDetected;
public PlayerHealth playerHealth;
void Start()
{
shootbullet = GetComponent<shootBullet>();
playerHealth = GameObject.FindWithTag("Player").GetComponent <PlayerHealth> ();
}
void Update()
{
if(playerHealth.currentHealth <= 0)
GetComponent<Animation>().CrossFade (enemyIdle.name);
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player"
{
PlayerDetected = true;
if (PlayerDetected)
{
transform.LookAt(Player);//Looks at the player
currentState = EnemyState.RUN;
transform.position += transform.forward*RunningSpeed*Time.deltaTime;//follows the player
}
}
else if(Vector3.Distance(transform.position,Player.position) > MaxDist)
{
RoamAround ();
}
if (Vector3.Distance (transform.position,Player.position) < MinDist)
{
currentState = EnemyState.ATTACK;
Attack ();
}
if (currentState == EnemyState.IDLE)
{
GetComponent<Animation>().CrossFade (enemyIdle.name);
}
else if (currentState == EnemyState.WALK)
{
GetComponent<Animation>().CrossFade (enemyWalk.name);
}
else if (currentState == EnemyState.RUN)
{
GetComponent<Animation>().CrossFade (enemyRun.name);
RunningSpeed = DefaultRunningSpeed;
}
else if (currentState == EnemyState.ATTACK)
{
GetComponent<Animation>().CrossFade (attack.name);
RunningSpeed = Stopped;
if(playerHealth.currentHealth <= 0)
GetComponent<Animation>().CrossFade (enemyIdle.name);
}
else if (currentState == EnemyState.KILL)
{
anim.Stop ("machinegunblast");
GetComponent<Animation>().CrossFade (enemyKill.name);
}
}
void RoamAround()
{
currentState = EnemyState.IDLE;//Add other RoamAround stuff here
}
public void Attack()
{
GetComponent<Animation>().CrossFade (enemyIdle.name); //shootbullet.enabled = true;//Add attacking stuff here
if(playerHealth.currentHealth <= 0)
GetComponent<Animation>().CrossFade (enemyIdle.name);
if(playerHealth.currentHealth <= 0)
currentState = EnemyState.ATTACK;
}
}
well, yeah, this script has references to other scripts that you don't have or need .It's template. I added Playerhealth scripts so the enemy would stop attacking when I was dead and the enemy health script is referencing the enemy that has this attack script. delete anything to do with playerhealth, enemyhealth or whatever else gives you an error