- Home /
Need to walk idly around unless ball is around
I've been struggling with this one for days now. I think I might be close, but I can't figure it out for the life of me. I need for the AI to wander around aimlessly (which it does) unless a ball is thrown on to the playing field. Then I need for it to try to get the ball. I have the getting the ball part and it works good. I have the wandering AI part which also works great. What I need help on is combining the two. I've tried if statements, but keep getting errors.
Here's what I have so far.
#pragma strict @script RequireComponent(CharacterController) var speed:float = 5; var directionChangeInterval:float = 1; var maxHeadingChange:float = 30; var heading: float=0; var targetRotation: Vector3 ; var Ball: GameObject; var target : Transform; var moveSpeed = 3; var rotationSpeed = 3; var myTransform : Transform; /// /// Creates wandering behaviour for a CharacterController. /// function Awake (){ myTransform = transform; // Set random initial rotation transform.eulerAngles = Vector3(0, Random.Range(0,360), 0); // look in a random direction at start of frame. //StartCoroutine NewHeadingRoutine (); } function Start() { var go = GameObject.FindWithTag("Ball"); } function Update (){ var go = GameObject.FindWithTag("Ball"); if (go == null) return; target = go.transform; myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; var controller : CharacterController = GetComponent(CharacterController); transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval); var forward = transform.TransformDirection(Vector3.forward); controller.SimpleMove(forward * speed); } /// <summary> /// Repeatedly calculates a new direction to move towards. /// Use this instead of MonoBehaviour.InvokeRepeating so that the interval can be changed at runtime. /// </summary> while (true){ NewHeadingRoutine(); yield WaitForSeconds(directionChangeInterval); } /// <summary> /// Calculates a new direction to move towards. /// </summary> function NewHeadingRoutine (){ var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360); var ceil = Mathf.Clamp(heading + maxHeadingChange, 0, 360); heading = Random.Range(floor, ceil); targetRotation = new Vector3(0, heading, 0); }
Are the erros occur on these lines:
var go = GameObject.FindWithTag("Ball"); if (go == null) return;
I actually don't get any errors. If I take out my ball code it will move around the scene just fine. If I use only my ball code it will go directly to the ball. I've been at a loss trying to get it to do both. Check the scene for a ball and if there is a ball to go to it. If not... Then mill around the scene.
Answer by KellyThomas · Mar 08, 2014 at 03:55 AM
It looks like you want your AI to be in one of several states i.e. either idle
or seek
but never both at the same time. This is a scenario that can be modeled with a Finite State Machine (FSM). These can be quite complex, or quite simple.
This is one implementation that uses an enum/switch based FSM:
enum AIState { Idle, Seek }
var state: AIState = AIState.Idle;
var target: Transform;
function Update() {
switch(state) {
case AIState.Idle:
UpdateIdle();
CheckIdleTransitions();
break;
case AIState.Seek:
UpdateSeek();
CheckSeekTransitions();
break;
}
}
function UpdateIdle() {
//perform idle actions
// ...
}
function CheckIdleTransitions() {
//check exit conditions
var go = GameObject.FindWithTag("Ball");
if (go != null) {
target = go.transform;
state = AIState.Seek;
}
}
function UpdateSeek() {
//perform seeking actions
// ...
}
function CheckSeekTransitions() {
//check exit conditions
// ...
}
Thank you so much! I wasn't sure how to do if else in Unityscript, but a state machine was the perfect solution! (Sometimes I'm just so close to the problem that I don't think of the right solutions!)
Thanks again! It works swim$$anonymous$$gly!
Your answer
Follow this Question
Related Questions
Enemy following Player in range 2 Answers
Help with Patrol Script!! 1 Answer
AI teleporting 1 Answer
A* optimization and path help 0 Answers
AI will complete patrolling and wait for 5 seconds 2 Answers