- Home /
Why does this script not work?
Hey there below is my AI script that i've been working on for the last few months. It all works except for the guard function. Unity doesn't detect any errors so why won't anything happen.
//Moving var moveSpeed = 3.0;
//Transform Variables var target : Transform; var bulletPrefab : Transform; var dead : Transform; var waypoint1 : Transform; var waypoint2 : Transform; var waypoint3 : Transform; var waypoint4 : Transform;
//Attacking var attackRange = 60.0;
function Start () { Guard (); }
function Guard () { transform.LookAt(waypoint1); transform.position += transform.forward moveSpeed Time.deltaTime;
distance = Vector3.Distance(transform.position, waypoint1.position);
if(distance <= 2.0)
{
transform.LookAt(waypoint2);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
distance = Vector3.Distance(transform.position, waypoint2.position);
if(distance <= 2.0)
{
transform.LookAt(waypoint3);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
distance = Vector3.Distance(transform.position, waypoint3.position);
if(distance <= 2.0)
{
transform.LookAt(waypoint4);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
distance = Vector3.Distance(transform.position, waypoint4.position);
if(distance <= 2.0)
{
transform.LookAt(waypoint1);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
function Attack () { var distance = Vector3.Distance(transform.position, target.position);
if(distance <= attackRange)
{
transform.LookAt(target);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
Yeah now it moves to the first waypoint but when it reaches the waypoint it just stops. Do you know why it won't move to the next. I've got a feeling it's to do with my if statements but I don't know how to fix the problem.
Answer by 3DMagicVR · Jan 20, 2011 at 05:17 AM
Hi, don't use the guard function on the start function because they only works at the start game (only one time), on the other side use the update function this way the guard all ways be checked for the distance of the objects and then if it's in any of the settled ranges this should be work.
function Start() // work at the beginning.
function Update() // work all ways.
I don't know how you activate the Attack function but I suggest to don't use in a Awake or Start function to check a function to have to be checked all the time.
Answer by · Jan 20, 2011 at 05:37 AM
By calling Guard()
in the Start()
function, it's only happening once. So, if any of those if
conditions are true on the first execution, they'll move ~0.48 units towards the waypoint. Otherwise, nothing will happen.
Oops, it said 0 answers when I loaded this tab, and then wandered away before posting...
Your answer
Follow this Question
Related Questions
Enemy AI monster problem 3 Answers
Follow up to AI Pathfinding Question 0 Answers
Bug with my enemyAI 2 Answers
Switching Enemy Behaviour through a set condition 0 Answers
How how can I make AI only move towards objects that are active in the scene 0 Answers