Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 noahcscott1142000 · Apr 18, 2018 at 01:35 PM · animationanimator controllercodepage

i need help with make an attack animation only when the player is near

//right now its stuck looping the attack animation at all times i cant tell if its a coding problem exactly or a animator controller set up problem help if possible.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;

public class EnemyAttack : MonoBehaviour { public float timeBetweenAttacks = 0.5f; public int attackDamage = 10;

 Animator anim;
 GameObject player;
 PlayerHealth playerHealth;
 //EnemyHealth enemyHealth;
 bool playerInRange;
 float timer;


 // Use this for initialization
 void Awake ()
 {
     player = GameObject.FindGameObjectWithTag("Player");
     playerHealth = player.GetComponent<PlayerHealth> ();
     //enemyHealth = GetComponent<EnemyHealth> ();
     anim = GetComponent <Animator> ();
     
     
 }
 
 // Update is called once per frame
 void OnTriggerEnter (Collider other)
 {
     if(other.gameObject == player)
     {
         playerInRange = true;
         anim.SetTrigger("Attack");
     }
 }

 void OnTriggerExit(Collider other)
 {
     if(other.gameObject == player)
     {
         playerInRange = false;
     }       
 }

 void Update()
 {
     timer += Time.deltaTime;
     if (timer >= timeBetweenAttacks && playerInRange/* && enemyHealth.currentHealth > 0*/)
     {
         Attack();
     }

      if (playerHealth.currentHealth <= 0)
     {
         Destroy(player);
     }
 }

 void Attack()
 {
     timer = 0f;

     if(playerHealth.currentHealth > 0)
     {
         playerHealth.TakeDamage(attackDamage);           
     }
 }

}

Comment
Add comment · Show 1
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 Jeppahx · Apr 18, 2018 at 02:00 PM 1
Share

Can you try to use anim.ResetTrigger() once the player is out of range?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Kciwsolb · Apr 18, 2018 at 04:23 PM

You have a few issues here. For one, when you set your timer plus equal to Time.deltaTime, you are setting it to the time it took to render the last frame. This might be causing the constant attacking. Also, you may want to call an Attack method in OnTriggerStay(Collider other) if other.gameObject is the player. By doing this, it will be (for the most part) called every frame that the player is touching the enemy trigger. You don't really need a bool for playerInRange that way.

 private void Attack() //Call this in OnTriggerStay if the colliding gameObject is the player
 {
         if(Time.time >= timer) //Check if current game time is at or past the time we last set
         {
                  playerHealth.TakeDamage(attackDamage); //make player take damage
                  animator.SetTrigger("Attack"); //trigger our animation
                  timer = Time.time + attackDelay; //Set timer to current game time plus our delay
         }
 }

Important!! Be sure in your AnimatorController there is a transition from your attack animation back to your idle animation. And be sure your attack animation clip does not have loop time set. Since you are triggering the animation, you only want it to play once until we trigger it again. So make sure it just transitions back to idle when it finishes.

Hope this helps get you going in the right direction.

Comment
Add comment · Show 1 · 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 Kciwsolb · Apr 18, 2018 at 05:10 PM 0
Share

Also, personally I would have the PlayerHealth script check if the currentHealth is

So you might have a TakeDamage method on PlayerHealth something like this:

 public void TakeDamage(int attackDamage) 
 {
         currentHealth -= attackDamage;
         if(currentHealth <= 0)
         {
                 Destroy(gameObject);
         }
 }

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

211 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

AnimClip Curve to Animator parameter 0 Answers

Unity UI Animation does not set end value 0 Answers

How can I sync animations between two avatars using the same animator controller? 0 Answers

Animator Controller 2D RPG Best Practices 0 Answers

Animtion looping if joystick is held 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