How can I get this script to work?

var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
private var isItAttacking = false;

    function Update () 
    {
    distance = Vector3.Distance(target.position, transform.position);
 
    if(distance < lookAtDistance)
    {
    isItAttacking = false;
    renderer.material.color = Color.yellow;
    lookAt ();
    }   
    if(distance > lookAtDistance)
    {
    renderer.material.color = Color.green; 
    }
    if(distance < attackRange)
    {
    attack ();
    }
    if(isItAttacking)
    {
    renderer.material.color = Color.red;
    }
}
 
 
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
 
function attack ()
{
    isItAttacking = true;
    renderer.material.color = Color.red;
 
    transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}

Hi everyone! So I have awesome script made by OrangeLightning -

In the original post it says to “drag the target you want the AI to follow inside the slot in the Inspector named “target”.” How would I do this if I want the player to be the target? I’m a beginner to scripting so i have no idea :slight_smile:

you should have an AI character with this script attached to it. You do that by dragging the script onto the AI character.

You then drag the Human Character onto the word target in the inspector.

If you don’t know how to create a character I would suggest looking at tutorials

unity3dstudent.com
3dbuzz.com

youtube channels including
pushypixel
3dbuzz

or anything that comes up typing unity tutorials