- Home /
Enemy Attacks not lining up properly, why?
Hi!
I'm writing a simple enemy AI for a game I'm making, and as of now it should just have two different attacks which would be picked randomly(even tho its just one or the other, but hey...) here's how it should be working:
I made a "dice" that will only generate a number when the player is in range, then stop. If the player is in range the enemy will start attacking based on what number was thrown and turn the dice back on afterwards. The dice will generate a new number and the next attack should follow.
It does this pretty nicely some times, however sometimes it simply keeps throwing numbers making the enemy use multiple attacks at the time. I can't seem to find out why tho, does anybody know where I went wrong?
Here's the script:
var player : GameObject;
var distToPlayer : float;
var attackDice : int;
var attackRange : float;
var canRoll = true;
function Start()
{
findPlayer();
attackRange = 15;
canRoll = true;
}
function Update ()
{
checkPlayerInRange();
if (distToPlayer > attackRange)
{
canRoll = false;
attackDice = 100;
}
if (canRoll != false)
{
rollDice();
}
}
function checkPlayerInRange()
{
var distance = Vector3.Distance(transform.position, player.transform.position);
distToPlayer = distance;
if (distToPlayer < attackRange)
{
Attack();
lookAtMe();
if(attackDice == 100)
{
attackDice = 6;
}
}
}
function findPlayer()
{
player = GameObject.FindWithTag("Player");
}
function Attack()
{
if (attackDice >5 && attackDice < 11)//use breath attack
{
flamethrower = transform.Find("Wisp");
animation.CrossFade("firebreath");
yield WaitForSeconds(1.5);
flamethrower.particleEmitter.emit = true;
if (distToPlayer < 10)
{
player.GetComponent("PlayerHealth").curHealth -= 1;
}
yield WaitForSeconds(1.5);
flamethrower.particleEmitter.emit = false;
attackDice = 0;
canRoll = true;
}
if (attackDice > 0 && attackDice < 6)//use sword attack
{
animation.CrossFade("swordslash");
if (distToPlayer < 5)
{
player.GetComponent("PlayerHealth").curHealth -= 3;
}
yield WaitForSeconds(2);
attackDice = 0;
canRoll = true;
}
else
{
animation.CrossFade("idle");
}
}
function rollDice()
{
var rolledDice : int = Random.Range(1,10);
attackDice = rolledDice;
canRoll = false;
}
function lookAtMe()
{
var lookPos = player.transform.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 2);
}
Answer by BiG · Dec 16, 2011 at 07:52 AM
I think that the problem relies on the absence of a lock, that will "block" the Update call when the action have already took place. In other words, I think that the Update procedure is a bit long to "stay" in a single frame; that is, you could have multiple calls to the Attack() function in a single frame, and the result is an enemy that attacks many times, even if his "turn" would be finished.
I would modify the script in the following way:
Declaring a boolean function at the beginning:
var lock=false;
Then, modify the Update() function this way:
function Update ()
{
if (lock==false){
lock=true;
checkPlayerInRange();
if (distToPlayer > attackRange)
{
canRoll = false;
attackDice = 100;
}
if (canRoll != false)
{
rollDice();
}
lock=false;
}
}
Hope that it works. Let me know...
Hi, I'm not sure, but that is what I was trying to do with the canRoll variable, but I guess this would work better, I'll try it out when I get home!:D
D: finally got to testing this out and it still seems to be doing some weird things.
At first they did seem to be lining up correctly, however after a while they still got strangled up, making the particle emitters ti$$anonymous$$g all wrong and not playing the sword animation completely every now and then.
I'm amazed at how hard it is to do this, all I really need to do is set some sort of small interval between attacks...
Anyways, thanks for trying, I'll just have to learn a bit more about how to make these things before I try this again 8D
Your answer
Follow this Question
Related Questions
Trouble with turret script 1 Answer
What is AutoWayPoint 1 Answer
[C#] Raycast based AI 2 Answers
Can you randomise a Navmesh agents rotation 1 Answer
Car Ai - Calculate max corner speed 1 Answer