-
-
Save LMichelle/eb824fe90b1f3d7d02e543ce371708f6 to your computer and use it in GitHub Desktop.
Enemy script from SlimeShooter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.AI; | |
using UnityStandardAssets.Characters.FirstPerson; | |
public class EnemySlimes : MonoBehaviour { | |
public GameObject Player; | |
public GameObject CookiePoint; | |
public MeshRenderer[] MeshRends; | |
private GameObject slimeUIPanel; | |
SlimeBar slimeScript; | |
private float PlayerToSlimeDist; | |
private float PlayerToCookieDist; | |
private float damping = 5; | |
public bool onPlayer = false; | |
public bool PlayerNearCookieWithSlime = false; | |
private bool once = true; | |
private float counter; | |
private bool slimeHasBeenOnCookie; | |
private bool noAttackPlayer; | |
/// <summary> | |
/// ----------------------------------------------------------------------------FUNCTIONS | |
/// </summary> | |
public void Start() | |
{ | |
Player = GameObject.FindGameObjectWithTag("Player"); | |
CookiePoint = GameObject.FindGameObjectWithTag("Cookie"); | |
MeshRends = GetComponentsInChildren<MeshRenderer>(); | |
slimeUIPanel = GameObject.FindGameObjectWithTag("SlimePanel"); | |
slimeScript = slimeUIPanel.GetComponent<SlimeBar>(); | |
} | |
public void FixedUpdate() | |
{ | |
PlayerToSlimeDist = Vector3.Distance(Player.transform.position, transform.position); | |
PlayerToCookieDist = Vector3.Distance(Player.transform.position, CookiePoint.transform.position); | |
if (onPlayer == false) | |
{ | |
if (PlayerToSlimeDist < 2 && PlayerNearCookieWithSlime == false && noAttackPlayer == false) | |
{ | |
AttackPlayer(); | |
once = true; | |
} | |
else if (PlayerToSlimeDist < 20 && PlayerNearCookieWithSlime == false){ | |
LookAndChasePlayer(); } | |
} else { // onPlayer is true | |
if (PlayerToCookieDist < 7) | |
{ | |
PlayerNearCookieWithSlime = true; | |
onPlayer = false; | |
} | |
} | |
if (PlayerToCookieDist < 7) | |
{ | |
noAttackPlayer = true; | |
} else | |
{ | |
noAttackPlayer = false; | |
} | |
if (PlayerNearCookieWithSlime == true) | |
{ | |
if (once) | |
{ | |
counter = 5.0f; | |
setNearCookie(); | |
once = false; | |
} | |
setCookieTimer(); | |
} | |
} | |
void LookAndChasePlayer() | |
{ // Will run as long as the player is nearby the slime | |
Quaternion rotation = Quaternion.LookRotation(Player.transform.position - transform.position); | |
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); | |
GetComponent<NavMeshAgent>().SetDestination(Player.transform.position); | |
} | |
void AttackPlayer() | |
{ | |
// Runs 1 time per slime. If the slime is too close to the player, then it should stick to the player - collision however is off. | |
Physics.IgnoreCollision(gameObject.GetComponent<Collider>(), Player.GetComponent<Collider>(), true); | |
foreach (MeshRenderer mesh in MeshRends) | |
{ | |
mesh.enabled = false; | |
} | |
slimeScript.slimeCounter += 1; | |
onPlayer = true; | |
} | |
void setCookieTimer() | |
{ | |
counter -= Time.deltaTime; | |
if (counter <= 0.0f) | |
{ | |
PlayerNearCookieWithSlime = false; | |
} | |
} | |
void setNearCookie() | |
{ | |
// First, set the meshrenderes en collision to enabled | |
Physics.IgnoreCollision(gameObject.GetComponent<Collider>(), Player.GetComponent<Collider>(), false); | |
foreach (MeshRenderer mesh in MeshRends) | |
{ | |
mesh.enabled = true; | |
} | |
// Then set the destination to the cookie. | |
gameObject.transform.position = Random.insideUnitSphere * 2 + CookiePoint.transform.position; | |
GetComponent<NavMeshAgent>().SetDestination(CookiePoint.transform.position); | |
slimeScript.slimeCounter -= 1; | |
onPlayer = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment