Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SomeRandomGuy · Dec 15, 2011 at 05:33 PM · ai

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
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...

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SomeRandomGuy · Dec 16, 2011 at 08:39 AM 0
Share

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

avatar image SomeRandomGuy · Dec 19, 2011 at 07:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges