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
1
Question by Wartorg · Jan 26, 2013 at 06:56 PM · javascriptaienemyattack

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);
 }
Comment
Add comment · Show 5
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 Taryndactyl · Jan 26, 2013 at 09:36 PM 0
Share

I believe you are not resetting your timer. It seems to remain at zero once it initially becomes 0.

avatar image Wartorg · Jan 26, 2013 at 10:57 PM 0
Share

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.

avatar image BBoulder · Jan 26, 2013 at 11:06 PM 0
Share

Dude, you gotta add some debug statements in there to see what's going out. Print the value of Timer before your if checks.

avatar image cscotttaakan · Jan 26, 2013 at 11:07 PM 0
Share

At line 66, pretty sure that it should be if (Timer > 0) Flip it to timer is greater than 0 Same with line 52

avatar image Wartorg · Jan 26, 2013 at 11:26 PM 0
Share

Haha, Thanks cscotttaakan, can't belive I didn't see that. :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · 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

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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