Skip to content

Instantly share code, notes, and snippets.

@Pikachuxxxx
Created June 24, 2020 09:43
Show Gist options
  • Save Pikachuxxxx/4795c6f4cba8b5b369f2e4b66c9a2185 to your computer and use it in GitHub Desktop.
Save Pikachuxxxx/4795c6f4cba8b5b369f2e4b66c9a2185 to your computer and use it in GitHub Desktop.
Unity Crowd NPC Behaviour - NPC type : Loner
using UnityEngine;
using UnityEditor;
public enum LonerStates
{
Moving,
Dying,
Exploring
}
public class Loner : NPC
{
[ReadOnly]
[SerializeField]
private LonerStates _lonerState;
public float explorationTime;
[ReadOnly] [SerializeField] private float currentTime;
private Area targetArea;
public LonerStates Loner_State
{
get
{
return _lonerState;
}
private set
{
_lonerState = value;
}
}
public override void Die(Action killAction, Vector3 LKPlayerPos)
{
base.Die(killAction, LKPlayerPos);
Loner_State = LonerStates.Dying;
}
private void Start()
{
#region Component Reference
#endregion
AssignArea(getPoint: true, moveNPC: true);
currentTime = explorationTime + 0.1f;
npcType = NPCType.Loner;
SetAreaIndices();
Loner_State = LonerStates.Exploring;
// #region Debug color
// Color background = new Color(
//Random.Range(0.5f, 1f),
//Random.Range(0.5f, 1f),
//Random.Range(0.5f, 1f));
// gameObject.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Transparent/Diffuse"));
// gameObject.GetComponent<MeshRenderer>().material.color = background;
// #endregion
}
public void Update()
{
//Just for testing purpose
if (Loner_State == LonerStates.Exploring)
{
if (currentTime > explorationTime)
{
AssignArea(getPoint: true, moveNPC: true);
_lonerState = LonerStates.Moving;
currentTime = 0f;
}
currentTime += Time.deltaTime;
}
if (pathComplete() && Loner_State != LonerStates.Dying)
{
//print("Completed");
Loner_State = LonerStates.Exploring;
}
else if (!pathComplete())
{
//print("Not Completed");
}
// Set player animations here
if (meshAgent.speed > 0 && Loner_State == LonerStates.Moving)
npcAnim.SetBool("isWalking", true);
else if (Loner_State == LonerStates.Exploring)
npcAnim.SetBool("isWalking", false);
}
void SetAreaIndices()
{
for (int i = 0; i < npcAreas.Length; i++)
{
npcAreas[i].areaIndex = i;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//void OnDrawGizmos()
//{
// DrawPath();
// Handles.color = Color.black;
// GUIStyle style = new GUIStyle();
// style.normal.textColor = Color.red;
// Handles.Label(new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z + 0.5f), "Loner State : " + _lonerState, style);
// Handles.Label(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z + 1f), "Selected Area Index: " + selectedAreaIndex, style);
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment