Hi all,
so I have this script for a simple AI thanks to this super tutorial of Holistic3D
so far the character is in Idle state (random picked from different idles), if you come near and are on sight it walks towards you and when it gets nearer it stops and attacks you.
thing is, I want that when the NPC is in Idle state (meaning i am not in range), sometimes it goes into a Patrolling state (just walking for some seconds in a random direction)… I am not super in scripting and after hours of search i could not manage much… any help would be much appreciated! thanks in advance!
I already set the Animator for the “isPatrolling” state… but I have no idea how to switch between “isIdle” and “isPatrolling”…
this is the basic script provided by Holistic3D
using UnityEngine;
using System.Collections;
public class chase : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction,this.transform.forward);
if(Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 5)
{
this.transform.Translate(0,0,0.05f);
anim.SetBool("isWalking",true);
anim.SetBool("isAttacking",false);
}
else
{
anim.SetBool("isAttacking",true);
anim.SetBool("isWalking",false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
}
You’re going to have to consider some sort of timer. So right now it’s default to idle. Let’s say you want it to idle for 3 secs. So you would idle and then while idling, you can add Time.DeltaTime to another value and then check to see if that value is greater than or equal to your idle time. Once it is, you’ll change it to patrol. After x time of patrolling, you change back to idle. It shouldn’t be to difficult to set up over all.
yes i feel super stupid because theoretically I know what I need but… I rly suck at scripting apparently because I can not get how to set it up…
I managed so far to have them from Idle to Patrolling… but they do not come back into Idle…
here is my code so far…
using UnityEngine;
using System.Collections;
public class Chase : MonoBehaviour {
private Transform player;
private Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
player = GameObject.FindWithTag("Player").transform;
}
void Update ()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle (direction, this.transform.forward);
//if it sees me and i am in range
if (Vector3.Distance (player.position, this.transform.position) < 10 && angle < 30) {
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,
Quaternion.LookRotation (direction), 0.1f);
anim.SetBool ("isIdle", false);
//if im in range but not it walks
if (direction.magnitude > 5) {
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
anim.SetBool ("isPatrolling", false);
}
//if im in range and near it meows
else {
anim.SetBool ("isAttacking", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isPatrolling", false);
}
}
//otherwise it go Idle
else {
StartCoroutine(IdleAndPatrolling());
}
}
IEnumerator IdleAndPatrolling(){
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
anim.SetBool ("isPatrolling", false);
yield return new WaitForSeconds(10);
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isPatrolling", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isAttacking", false);
anim.SetBool ("isWaiting", false);
}
}
First, running a coroutine from Update is generally bad, unless you account for it, you are actually running a call to start the coroutine once every frame. Which means that if for example, you have 60 frames a sec, you would call that coroutine 600 times during the 10 sec wait, which also means you’ll get some strange behavior from this.
You could have a coroutine that simply switches between two states, but only if it’s not suppose to be attacking. While probably not the best design, you could have it do another wait at the end and then start the coroutine over again. Just make sure you don’t try to idle/patrol while it’s suppose to be attacking.
1 Like