- Home /
Solved it myself.
Why does the script act completely wrong?
Hi there. I am making a hello neighbor clone and i have tried coding a sort of a "State Machine" for the neighbor's AI. I tried basic stuff for now, such as simple patrolling and stretching. However after toying around with this script for quite some time, its seemingly still broken.
The neighbor right now has 3 functional states: Idle, Stretching and Patrolling.
Expected behaviour: Random state is chosen on start. If starts Idle, he waits a certain time of a variable (TimeToWaitAtWaypoint). Then, chooses a random one again. If starts with Stretching, he is supposed to do the animation, and choose a random one again. If patrolling, he is supposed to choose a random waypoint from the array, go to it and when he reaches it, trigger the random state function again.
In-Game behaviour: The neighbor starts out seemingly in tact if he starts with idle or stretching, however if he starts with patrolling, he starts rapidly choosing random waypoints and constantly changing the state to another random one. If he ends up with Idle or Stretching, he does stretch and wait a while, but then when supposed to trigger the random state function, it starts flickering between them all and rapidly choosing random states. I cant figure out why, can someone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NeighborAI : MonoBehaviour
{
//Navigation
public GameObject[] Waypoints;
public float neighborWalkSpeed;
public float neighborRunSpeed;
public NavMeshAgent navAgent;
public GameObject currentDestination;
public float TimeToWaitAtWaypoint;
//Animation
public Animator anim;
//Actions
public AIState _AIState;
public float MinIdleTime;
public float MaxIdleTime;
//Sound
public AudioSource voice;
public float volume;
public GameObject chaseMusic;
//Sound clips
public AudioClip SpotPlayer;
public AudioClip cleanThroat;
public AudioClip laugh01;
public float minRandomVoiceTime;
public float maxRandomVoiceTime;
bool CanStartVoiceRoullete;
//State Function variables
public bool startedIdleStateFunction;
public bool startedStretchingStateFunction;
void Start()
{
RandomBehaviourFunc();
}
public void RandomBehaviourFunc()
{
Debug.Log("Random Behaviour is being chosen!");
//Random Behaviour
startedIdleStateFunction = false;
startedStretchingStateFunction = false;
int RandomBehaviour = Random.Range(1, 3);
if (RandomBehaviour == 1)
{
Debug.Log("State set to Idle");
_AIState = AIState.Idle;
StartCoroutine(delayedWaypointChoose());
}
else if (RandomBehaviour == 2)
{
Debug.Log("State set to stretching.");
_AIState = AIState.Stretching;
}
else if (RandomBehaviour == 3)
{
Debug.Log("State set to patrolling.");
_AIState = AIState.Patrolling;
}
}
public enum AIState
{
//The AI's states
Idle,
Stretching,
Chasing,
Patrolling,
Searching
}
void Update()
{
//Waypoint detection
Waypoints = GameObject.FindGameObjectsWithTag("NeighborWaypoint");
//Walk Speed
if(_AIState == AIState.Chasing || _AIState == AIState.Searching)
{
navAgent.speed = neighborRunSpeed;
} else
{
navAgent.speed = neighborWalkSpeed;
}
//Patrolling
if(_AIState == AIState.Patrolling && _AIState != AIState.Chasing && _AIState != AIState.Stretching && currentDestination == null)
{
ChooseWaypoint();
}
if (currentDestination != null)
{
navAgent.destination = currentDestination.transform.position;
}
//Stretching
if(_AIState == AIState.Stretching)
{
anim.SetBool("isStretching", true);
anim.SetBool("isIdle", false);
if (startedStretchingStateFunction == false)
{
if (_AIState != AIState.Patrolling)
{
StartCoroutine(StretchState());
startedStretchingStateFunction = true;
}
}
}
//Idle
if (_AIState == AIState.Idle)
{
StartCoroutine(IdleState());
}
//Random voice clips during his idling
if(CanStartVoiceRoullete)
{
int chance = Random.Range(0, 30);
if(chance == 19)
{
StartCoroutine(voiceRoullete(0));
}
}
//Animation
if(navAgent.velocity.magnitude > 0.1 && _AIState != AIState.Chasing && _AIState != AIState.Stretching)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
} else if(navAgent.velocity.magnitude < 0.1)
{
if (_AIState != AIState.Stretching)
{
anim.SetBool("isIdle", true);
}
}
}
public void ChooseWaypoint()
{
currentDestination = Waypoints[Random.Range(0, Waypoints.Length)];
_AIState = AIState.Patrolling;
}
IEnumerator voiceRoullete (int voiceClip)
{
yield return new WaitForSeconds(Random.Range(minRandomVoiceTime, maxRandomVoiceTime));
voiceClip = Random.Range(1, 2);
if(voiceClip == 1)
{
voice.PlayOneShot(cleanThroat, volume);
} else if (voiceClip == 2)
{
voice.PlayOneShot(laugh01, volume);
}
CanStartVoiceRoullete = true;
}
IEnumerator delayedWaypointChoose()
{
yield return new WaitForSeconds(TimeToWaitAtWaypoint);
ChooseWaypoint();
}
IEnumerator StretchState()
{
currentDestination = null;
yield return new WaitForSeconds(10.5f);
RandomBehaviourFunc();
}
IEnumerator IdleState()
{
currentDestination = null;
yield return new WaitForSeconds(Random.Range(MinIdleTime, MaxIdleTime));
RandomBehaviourFunc();
}
public void OnTriggerEnter(Collider other)
{
//Upon entering a waypoint
if(other.gameObject == currentDestination)
{
Debug.Log("Arrived at destination");
RandomBehaviourFunc();
}
}
}
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Behavior Tree Barebones Tutorial 5 Answers
Multiple Cars not working 1 Answer
Enemy pick and throw an object 1 Answer
AI scripting Question. 2 Answers