It’s like they’re running on the same thread, this is the code I made
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using RAIN.Core;
using RAIN.Action;
using RAIN.Memory;
public class combatStatusChanges : MonoBehaviour {
public AudioClip weaponShot;
public GameObject rayOrigin;
public GameObject target;
AIGunHandling AIGunReference;
public AIRig currentAI;
bool isFiring = false;
public static bool startEngaging = false;
MemoryObject isInCover;
MemoryObject targetNull;
bool canChangeBehavior = true;
// Use this for initialization
void Start () {
currentAI.AI.Mind.AI.WorkingMemory.SetItem<bool>("fire", false);
}
// Update is called once per frame
void Update () {
targetNull = currentAI.AI.Mind.AI.WorkingMemory.GetItem("varHero");
isInCover = currentAI.AI.Mind.AI.WorkingMemory.GetItem("closeCover");
Debug.Log(targetNull);
if(currentAI.AI.Senses.GetSensor("Eyes").AI.WorkingMemory.GetItem("varHero") != null)
{
if(UnityEngine.Random.Range(0, 100) >= 50 && canChangeBehavior)
{
currentAI.AI.Mind.AI.WorkingMemory.SetItem<bool>("canFire", true);
if(!isFiring)
StartCoroutine(fire());
StartCoroutine(waitForBehaviorChange());
}
else if(canChangeBehavior)
{
currentAI.AI.Mind.AI.WorkingMemory.SetItem<bool>("canFire", false);
StartCoroutine(waitForBehaviorChange());
}
}
}
IEnumerator waitForBehaviorChange()
{
canChangeBehavior = false;
yield return new WaitForSeconds(2f);
canChangeBehavior = true;
}
IEnumerator fire()
{
isFiring = true;
transform.LookAt(target.transform);
Ray ray = new Ray(rayOrigin.transform.position, transform.forward);
RaycastHit hit;
audio.PlayOneShot(weaponShot);
Debug.DrawRay(ray.direction, transform.position);
if(Physics.Raycast(ray, out hit, 2000))
{
if(hit.collider.tag == "Player")
{
Debug.Log("Player hit");
}
}
yield return new WaitForSeconds(1 / 14);
isFiring = false;
}
}
And when the waitForBehaviorChange() coroutine is called the fire() coroutine is paused until the first one is done, in fact the model looks at the player every 2 seconds.
How can I fix this? Thanks for the help.