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 /
  • Help Room /
avatar image
0
Question by jem77fp · Feb 08, 2017 at 10:56 AM · animationstriggers

Long question about animation and triggers working strange

Hi there. Im working on creating plants vs zombies clone (undemy courses)i have 2 attackers: lizard and fox. lizard has attacker, health and lizard script on it. fox got attacker, health and fox script. all animations are ready and work fine atm. i enter playmode and my fox jumps over the stone and goes further until she reaches defender. then fox begins to attack and defender is losing health. lizard works great 2. he attacks all defenders including stones. but after stone\defender is destroyed, they stuck in attck mode and isAttacking animation keeps going on both attackers. and its fine atm. for fixing that i put this piece of code in ATTAKCER script in update function(doing this according to what i see in udemy tutorial)

  if (!currentTarget)
             {
                 animator.SetBool("isAttacking", false);
             }     

and heres there problems begin((( only one of my attackers in scene works fine( the last placed in scene) if lizard work bad - he ignores all stones and defenders and moves through it( but all triggers work well according 2 debugger) and if fox works bad- she still jumps over the stones, but ignore defenders and path through it. Debug.Log shows me that every collision is detected . but animation isnt triggered. if i leave only 1 attacker in my scene - it works correct, but 2+ attackers still giving me only 1 working fine. maybe its a long stupid question> but i really need help)) thanks a lot!

 public class Attacker : MonoBehaviour {
 
     private Animator animator;
     private GameObject currentTarget;
     private float currentSpeed;
     // Use this for initialization
     void Start ()
     {       
         animator = GetComponent<Animator>();       
     }
     void Update ()
     {
         
         transform.Translate(Vector3.left * currentSpeed * Time.deltaTime);
     }
 
     void OnTriggerEnter2D()
     {    }
 
     public void WalkSpeed(float speed)
     {
         currentSpeed = speed;
     }
 
     public void StrikeCurrentTarget(float damage)
     {
         if (currentTarget)
         {
             Health health = currentTarget.GetComponent<Health>();            
             if (health)
             {
                 health.dealDamage(damage);
             }
         }
         
     }
     public void Attack(GameObject obj)
     {
         currentTarget = obj;
     }
 }

Health script:

 public class Health : MonoBehaviour {
 
     public float health = 50.0f;
 
     public void dealDamage(float damage)
     {
         health -= damage;
         if(health <= 0)
         {
             DestroyObject();
         }
     }
     public void DestroyObject()
     {
         Destroy(gameObject);
     }
 }

lizard has its own script LIzard:

 public class Lizard : MonoBehaviour
 {
     private Attacker attacker;
     private Animator animator;
    
     void Start()
     {
         animator = GetComponent<Animator>();
         attacker = FindObjectOfType<Attacker>();
     }
 
     void Update()
     { }
 
     void OnTriggerEnter2D(Collider2D coll)
     {
         GameObject obj = coll.gameObject;
         if (!obj.GetComponent<Defender>()) //ignores all but defenders
             return;
         }
         else
         {
             animator.SetBool("isAttacking", true);
             attacker.Attack(obj);
         }
     }
 }

and here is the fox script

 public class Fox : MonoBehaviour {
 
     private Attacker attacker;
     private Animator animator;
 
     // Use this for initialization
     void Start()
     {
         animator = GetComponent<Animator>();
         attacker = FindObjectOfType<Attacker>();
     }
   
     void Update()
     {      }
 
     void OnTriggerEnter2D(Collider2D coll)
     {
         GameObject obj = coll.gameObject;
         if (!obj.GetComponent<Defender>()) //ignores all but defenders
         {
             return;
         }
         if (obj.GetComponent<Stone>()) //breaking stones aswell
         {
             animator.SetTrigger("Jump Trigger");          
         }
         else
         {           
             animator.SetBool("isAttacking", true);
             attacker.Attack(obj);
         }
     }
 }








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

0 Replies

· Add your reply
  • Sort: 

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

91 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

Related Questions

Triggering animations in set areas by Key Press? 1 Answer

Trigger the Animation #2 after Animation #1 was triggered 0 Answers

How would I switch animators on an object via a trigger 0 Answers

Get hand and gun animation in sync 0 Answers

Disabling object in animation 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