- Home /
Need Help Understanding Script! (Javascript)
Ok, So i am using an AI script in my game and i need a little help understanding a few of the components of the scrip. I am following a video tutorial series and i am trying to understand more about what the script means and how it works with the objects in game. (basically i did what the man in the tutorial did and i need some help understanding what is going on)
First off, what is the characterController variable doing? The AI object is supposed to move towards me and even when i am in range of it, it just does move at all! When in unity under the property inspector the script has an option to add some sort of controller to the character controller spot, but when i do that i can go within the lookatdistance. (see code below to answer)
If you know any other reason why it is not chasing me, please help it would be much appreciated.
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
renderer.material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
renderer.material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
After you attach this script to the AI, make sure to pick the AI object and check to make sure all of the speed values and everything is set. And most definitely make sure the Player prefab/object is assigned to the Target variable
Answer by Foestar · Apr 11, 2014 at 03:49 AM
Read it in small snippets. First you have your variables.
Then in your update you have a variable being defined based off an objects transform. The distance is then determined between the desired object and the current object.
Then you have it check the distance which will then determine it's next action.
The first action is to look at the object. This is done with the Quaternion rotations. Essentially, it will just rotate the object to look at the controlled object.
Next action is the attack which then moves the object forwards toward the other one. But this only activates if the objects are in range.
Now as for the target, it is defined by adding a object in the inspector window that you were asking about.
I have the target selected in unity as myself but it still doesnt move me...is there something with the character controller that needs to take place to follow me? or is the script just written wrong?
Thanks for telling me what it meant, i did a little more exploring and found that i had the wrong controller on the AI!
Glad you figured it out. The script is correct at first glance. It's just a matter of what it's attached to and what "Target" is defined as.