Im using the states i used for a different game, according to that script it is suppose to work.
I made a button which could switch the states manually. But none of them are actually switching the states . tho the debug.log is called upon - the inspector doesnt show a state change
My goal:
I woud like the monster with this script attached to
Stay idle
Be alerted
Going in for attack
Currently the monster moves to the player, i wanted to change so he wouldnt move if the state is diferrent (thats why the elkse statemebnt says movespeed =0 but that doesnt seem to work either, it just sticks to the state.attack
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public enum MonsterState
{
unaware,
alerted,
attack
} ;
//______variabelen________________________
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public AudioClip au_monsterAtt;
public int maxDistance;
public int minDistance;
public MonsterState gameState;
private Transform myTransform;
private CharacterController controller;
//_____awake______________________________
void Awake ()
{
myTransform = transform;
}
//_____start______________________________
void Start ()
{
GameObject go = GameObject.FindGameObjectWithTag ("Player");
gameState = MonsterState.attack;
target = go.transform;
maxDistance = 10;
minDistance = 5;
controller = GetComponent<CharacterController> ();
}
// _______onGUi___________________________
void OnGUI ()
{
switch (gameState)
{
case MonsterState.unaware:
if (GUI.Button (new Rect (Screen.width - 300,Screen.height - 350, 100, 30), "unaware"))
{
gameState = MonsterState.alerted;
Debug.Log ("switch to alerted");
}
break;
case MonsterState.alerted:
if (GUI.Button (new Rect (Screen.width - 300,Screen.height - 350, 100, 30), "alerted"))
{
gameState = MonsterState.attack;
Debug.Log ("switch to attack");
}
break;
case MonsterState.attack:
if (GUI.Button (new Rect (Screen.width - 300,Screen.height - 350, 100, 30), "attack"))
{
gameState = MonsterState.unaware;
Debug.Log ("switch to unaware");
}
break;
}
}
//_____update_____________________________
void Update ()
{
switch (gameState) {
case MonsterState.unaware:
Unaware ();
break;
case MonsterState.alerted:
Alerted ();
break;
case MonsterState.attack:
Attack ();
break;
}
}
private void Unaware()
{
//idle
}
private void Alerted()
{
}
private void Attack ()
{
if (gameState == MonsterState.attack)
{
// dit berekent de move tussen enemy en player - bepalend op de mindistance
if (Vector3.Distance (target.position, myTransform.position) < minDistance)
{
Vector3 moveDirection = target.transform.position - transform.position;
moveDirection.Normalize ();
moveDirection.y = 0;
controller.Move (moveDirection * moveSpeed * Time.deltaTime);
transform.LookAt (transform.position + moveDirection);
}
}
else
{
Debug.Log ("else statement");
moveSpeed = 0;
}
}
}