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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Checking name from hit from Raycast in array 2 Answers

Collsions problem 0 Answers

Top Down Shooter Enemy AI using raycasting 1 Answer

Make enemy more intelligent 0 Answers

Collisions not working when player stands still 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges