- Home /
Melee range AI
Every thing in the script runs fine until after the first attack. After the first attack the "Enemy" is suppose to attack again after some seconds but does not. when the timer is more than zero I want it to do the "timer -= Time.deltaTime", but it stays at one.
Here is the script:
var player : Transform;
var speed = 3.0;
var range = 20.0;
var hitRange = 3.0;
var enemyDamage = 100.0;
var Timer : float;
var Cooler : float = 1;
var rotationSpeed : int = 1;
var AttackTime : float = 1;
var TimerTwo : float;
function Awake ()
{
//find the player
player = GameObject.FindWithTag("Player").transform;
//Don't fall!
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
}
function Update()
{
//move towards player
var distance = Vector3.Distance( player.transform.position, transform.position);
if (distance > range)
{
return;
}
else if(distance < hitRange)
{
if(Timer == 0)
{
if(TimerTwo == 0)
{
Attack();
}
}
}
else
{
Move();
}
//Look at the player
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.position - transform.position), rotationSpeed * Time.deltaTime);
//if timer < 0 than withdraw one every sec.
if(Timer < 0)
{
Timer -= Time.deltaTime;
}
//Make sure Timer does not go below zero
if(Timer < 0)
{
Timer = 0;
}
//Same as above, this time with TimerTwo
if(Timer < 0)
{
TimerTwo -= Time.deltaTime;
}
if(TimerTwo < 0)
{
TimerTwo = 0;
}
}
function Attack ()
{
//insert attack anim here!
Move();
TimerTwo = Cooler;
yield WaitForSeconds (AttackTime);
print("ARRR");
Timer = Cooler;
}
function Move ()
{
var delta = player.transform.position - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}
I believe you are not resetting your timer. It seems to remain at zero once it initially becomes 0.
No, though while writing my response I found the solution to another error, so thank you for that :). I have edited my question so it explains the error a bit better.
Dude, you gotta add some debug statements in there to see what's going out. Print the value of Timer before your if checks.
At line 66, pretty sure that it should be if (Timer > 0) Flip it to timer is greater than 0 Same with line 52
Haha, Thanks cscotttaakan, can't belive I didn't see that. :D
Answer by Wartorg · Jan 26, 2013 at 11:34 PM
if(Timer < 0)
{
TimerTwo -= Time.deltaTime;
}
Should have been:
if(Timer > 0)
{
TimerTwo -= Time.deltaTime;
}
Thanks, cscotttaakan.
Your answer
Follow this Question
Related Questions
Enemy detection in the light(Javascript) 1 Answer
Enemy AI Problem: Dancing? 1 Answer
Enemy AI With changing Player 0 Answers
Waypoint System help 1 Answer
The enemy don´t lose health, but why??? 0 Answers