Hey guys, i’m fairly new to scripting and currently have a AI script that notices when your close then follows when you get closer and stops chasing when you run away. However i am having trouble making it attack and decrease the health. Here are the the scripts, appreciate the help and i know this has been asked before, i spent the last hour trying to find a solution…
AI:
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;
var TheDammage = 40;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
attackTime = Time.time;
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
function lookAt ()
{
renderer.material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
renderer.material.color = Color.red;
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
if (Distance < attackRange Time.time > attackTime)
{
attackTime = Time.time + attackRepeatTime;
Debug.Log(“i’m attacking you”);
}
}
function attack ()
{
if(attackRange < 1.5)
{
renderer.material.color=Color.blue;
}
}
Health:
#pragma strict
public static var health : float = 100;
function OnGUI()
{
GUI.Label(Rect(Screen.width-200,Screen.height-50,200,20),"Player Health " + health);
}