0% found this document useful (0 votes)
25 views2 pages

Frendlies

This document contains a script for enemy behavior in a game. The script defines enemy states like idle, walk, run, attack and sets animations and speeds for each state. It checks the distance to the player and transitions between states accordingly, chasing or attacking the player depending on their proximity.

Uploaded by

shrey pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Frendlies

This document contains a script for enemy behavior in a game. The script defines enemy states like idle, walk, run, attack and sets animations and speeds for each state. It checks the distance to the player and transitions between states accordingly, chasing or attacking the player depending on their proximity.

Uploaded by

shrey pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

sing 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;
}
}

You might also like