x


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);
}
more ▼

asked Dec 15 '11 at 05:33 PM

SomeRandomGuy gravatar image

SomeRandomGuy
152 14 28 30

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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...

more ▼

answered Dec 16 '11 at 07:52 AM

BiG gravatar image

BiG
4.8k 4 14 51

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

Dec 16 '11 at 08:39 AM SomeRandomGuy

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 timing 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

Dec 19 '11 at 07:36 PM SomeRandomGuy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x983

asked: Dec 15 '11 at 05:33 PM

Seen: 474 times

Last Updated: Dec 19 '11 at 07:36 PM