0% found this document useful (0 votes)
9 views7 pages

Enemy Old Script

The document is a C# script for an enemy character in a Unity game, detailing its properties, behaviors, and interactions with the player and other allies. It includes methods for taking damage, roaming, finding safe spaces, healing, and managing animations based on movement. The script also tracks various states such as whether the enemy is dead, safe, or has captured specific points in the game environment.

Uploaded by

William Conroy
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)
9 views7 pages

Enemy Old Script

The document is a C# script for an enemy character in a Unity game, detailing its properties, behaviors, and interactions with the player and other allies. It includes methods for taking damage, roaming, finding safe spaces, healing, and managing animations based on movement. The script also tracks various states such as whether the enemy is dead, safe, or has captured specific points in the game environment.

Uploaded by

William Conroy
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/ 7

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour


{
[SerializeField] private int EnemyHP = 100;
private Animator animator;

private UnityEngine.AI.NavMeshAgent navAgent;

public Transform player;

public Transform currentPos;

public Transform orientation;

public bool WeaponIsIdle;

public bool isDead;


public bool wellHealed;

public bool atPoint;


public bool isSafe;

public float captureDelay;


public float NewPointToAdvanceFindingDelay;
public float healingDelay;

public LayerMask whatIsPlayerAlly, whatIsPlayer, whatIsCurrentAlly;

public float allyDetectionRange, playerAllyDetectionRange, attackRange;


public bool playerInRange, alliesInAttackRange, playerInSightRange;

public int currentNumberofAllies;


public int currentNumberofPlayerAllies;

public float deathDelay;

public bool allPointsCaptured;


public bool PointACaptured;
public bool PointBCaptured;
public bool PointCCaptured;
public bool PointDCaptured;
public bool PointECaptured;
public bool PlayerSpawnCaptured;

public Transform ACube, BCube, CCube, DCube, ECube, PSCube, ESCube;

public Vector3 PointAVector;


public Vector3 PointBVector;
public Vector3 PointCVector;
public Vector3 PointDVector;
public Vector3 PointEVector;
public Vector3 PlayerSpawnVector;
public Vector3 SpawnpointVector;
public Transform rayCastPoint;
public float campMaxDistance = 10f;
public float obstacleDistance = 0f;

void Start()
{
animator = GetComponent<Animator>();
navAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
player = GameObject.Find("PlayerBodyobj").transform;
isDead = false;
isSafe = true;
}

public void TakeDamage(int damageAmount)


{
if (isDead == false)
{
EnemyHP -= damageAmount;
if (EnemyHP < 1)
{
int randomDeathValue = Random.Range(0,11); //0 to 10

if (randomDeathValue == 0)
{
animator.SetTrigger("DIE");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 1)
{
animator.SetTrigger("DIE1");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 2)
{
animator.SetTrigger("DIE2");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 3)
{
animator.SetTrigger("DIE3");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 4)
{
animator.SetTrigger("DIE4");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 5)
{
animator.SetTrigger("DIE5");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 6)
{
animator.SetTrigger("DIE6");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 7)
{
animator.SetTrigger("DIE7");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 8)
{
animator.SetTrigger("DIE8");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 9)
{
animator.SetTrigger("DIE9");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
if (randomDeathValue == 10)
{
animator.SetTrigger("FUNNYDEATH");
isDead = true;
animator.SetBool("ISDEAD", true);
Invoke("DEATH", deathDelay);
}
}

else
{
animator.SetTrigger("DAMAGE");
print("Health is now at " + EnemyHP + ".");
}

if (isDead == false && EnemyHP < 30)


{
wellHealed = false;
FindSafeSpace();
}
}
}

public void Roam()


{
RaycastHit hitblockage;
if(Physics.Raycast(rayCastPoint.transform.position,
rayCastPoint.transform.forward, out hitblockage, campMaxDistance))
{
obstacleDistance = hitblockage.distance;
}
obstacleDistance = 0;

if (obstacleDistance <= campMaxDistance)


{

void DEATH()
{
animator.SetBool("HASBEENKILLED", true);
}

void Update()
{
if (isDead == false)
{

//Orientation
Vector3 viewDir = currentPos.position - new Vector3(transform.position.x,
currentPos.position.y, transform.position.z);
orientation.forward = viewDir.normalized;

if (navAgent.velocity.magnitude > 0.1)


{
animator.SetBool("isSprinting", false);
animator.SetBool("isCrouching", true);
animator.SetBool("isWalking", false);
}

if (navAgent.velocity.magnitude < 0.1)


{
animator.SetBool("isSprinting", false);
animator.SetBool("isCrouching", false);
animator.SetBool("isWalking", false);
}

if (navAgent.velocity.magnitude > 3.1)


{
animator.SetBool("isSprinting", false);
animator.SetBool("isCrouching", false);
animator.SetBool("isWalking", true);
}

if (navAgent.velocity.magnitude > 6.1)


{
animator.SetBool("isSprinting", true);
animator.SetBool("isCrouching", false);
animator.SetBool("isWalking", false);
}
if (PointACaptured && PointBCaptured && PointCCaptured && PointDCaptured &&
PointECaptured)
{
allPointsCaptured = true;
}
else
{
allPointsCaptured = false;
}

if (allPointsCaptured == true && PlayerSpawnCaptured == true)


{
//GameOver
}

else
{
print("EnemyisDead");
}

}
}

void FindSafeSpace()
{
if (isDead == false)
{
navAgent.SetDestination(ESCube.position);
GetComponent<UnityEngine.AI.NavMeshAgent>().speed = 8;

Vector3 distanceToPoint = transform.position - ESCube.position;

//Walkpoint reached
if (distanceToPoint.magnitude < 1f)
isSafe = true;
Invoke("HealingInSafeSpace", healingDelay);
}
}

void HealingInSafeSpace()
{
if (isDead == false)
{
EnemyHP = EnemyHP + 30;
if (EnemyHP < 70)
Invoke("HealingInSafeSpace", healingDelay);
}

void Retreat()
{

}
void StandOff()
{

void ChargePlayerSpawn()
{
if (isDead = false)
navAgent.SetDestination(PSCube.position);
}

/*/
Making the Standoffs, attacking and retreating work.
int CountNearbyAllies()
{
//I would consider changing this to OverlapSphereNonAlloc to reduce gc
stutter
var hits = Physics.OverlapSphere(this.transform.position,
allyDetectionRange, whatIsCurrentAlly); //use those customizable variables instead
int cnt = 0;

//one fatal flaw here is that if the enemy consists of more than 1 collider
tagged 'enemy', it'll count multiple times.
//you may want to consider some method of relating an enemy all together as
a single entity.
foreach(var c in hits)
{
if(c.CompareTag("Enemy")) //use CompareTag, not tag ==
{
cnt++;
}
}

return cnt;
}

int CountNearbyPlayerAllies()
{
//I would consider changing this to OverlapSphereNonAlloc to reduce gc
stutter
var hits = Physics.OverlapSphere(this.transform.position,
playerAllyDetectionRange, whatIsPlayerAlly); //use those customizable variables
instead
int cnt = 0;

//one fatal flaw here is that if the enemy consists of more than 1 collider
tagged 'enemy', it'll count multiple times.
//you may want to consider some method of relating an enemy all together as
a single entity.
foreach(var c in hits)
{
if(c.CompareTag("PlayerAlly")) //use CompareTag, not tag ==
{
cnt++;
}
}

return cnt;
}
/*/

Old script

You might also like