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 Shentoza · Oct 08, 2015 at 01:16 PM · trigger2d-platformeranimations

Syncing Shot with Animation

So I'm currently making a 2D platformer game with unity, and our character is able to shoot. And we have 3 animations, 1 for preparing a shot, 1 for the actual shot, and then the preparation animation played backwards.

And I even managed to create mecanim states that work pretty good, the thing is now, my bullet spawns immediately. So how can i make my shoot script wait, until it is in the "Attack" State, at a certain keyframe, so i can assure, the shot is correctly synced up with the animation?

The problem is, i could use events and put the event at the fitting keyframe, but i want to use our shooting script also for enemies. So i can control it one time with our input system, and one time controlled by our AI script for enemies.

This is how our code in our shot method currently looks.

 if (rangeAttackCooldown[0] >= rangeAttackCooldown[1])
         {
             if(anim!=null)
                 anim.SetTrigger("Shot");
             GameObject proj = PPS.getProjectile();
             if (proj != null)
             {
                 currentProjectile = proj.GetComponent<Projectile>();
                 rangeAttackCooldown[0] = 0;
                 if (is_normal_shot)
                     currentProjectile.set_shooting_type(Projectile.Shooting_Type.NORMAL);
                 else
                     currentProjectile.set_shooting_type(Projectile.Shooting_Type.SPECIAL);
                 currentProjectile.shoot(2.0f, facingRight);
             }
         }

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

2 Replies

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

Answer by Shentoza · Oct 10, 2015 at 05:50 PM

Allright i got it. My Code in my input system:

 //Funktion für Schießen
         if (Input.GetKeyDown("s"))
         {
             StartCoroutine(actions.shoot(true));
         }
 
         if (Input.GetKeyDown("p"))
         {
             StartCoroutine(actions.shoot(true));
         }


My shooting Method

 public IEnumerator shoot(bool is_normal_shot)
     {
         if (rangeAttackCooldown[0] >= rangeAttackCooldown[1])
         {
             if(anim!=null)
                 anim.SetTrigger("Shot");
             while (!shotAnimationReady)
             {
                 yield return null;
             }
             GameObject proj = PPS.getProjectile();
             shotAnimationReady = false;
         }
     }




And my "readyShot" which is called from the keyframe:

 //Methode auf die der Keyframe zugreift, um den Schuss zu ermöglichen
     public void setShotAnimationReady()
     {
         shotAnimationReady = true;
     }



This does it for my problem.

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

Answer by OctoMan · Oct 08, 2015 at 02:26 PM

You have basicly 2 possibiltys. Use the animation event system to instantiate the bullet at the right frame, or you create some offset variables and use an ienumerator to use them.

 private IEnumerator ranged_attack()
     {
         is_attacking_ranged = true;
         {
             yield return new WaitForSeconds(attack_speed);
             Instantiate(Bullet, BulletSpawnPoint.position, BulletSpawnPoint.rotation);
             yield return new WaitForSeconds(attack_offset);
         }
         is_attacking_ranged = false;
     }

like this.
attackspeed is the space before the actual bullet is instantiated, attack_offset to complete the rest of the animation if there is any.

Cheers

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 Shentoza · Oct 10, 2015 at 04:09 PM 0
Share

Thanks for the help! Really appreciate it

Alright here is what i was planning to do. Add a "shotReady" method, which is called at the appropiate keyframe. And let my Shoot method wait until the shotReady bool has been set to true by that method. I changed my "public void Shoot" to "public IEnumerator shoot", but how do i call that method from my input system?

It used to be

 if (Input.Get$$anonymous$$eyDown("s"))
         {
             actions.shoot(true);
         }

and i changed it to

 if (Input.Get$$anonymous$$eyDown("s"))
         {
             yield return StartCoroutine("shoot");
         }

but it doesnt quite work, and i can't set the bool argument. How to use Coroutines properly?

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

33 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

Related Questions

Help with non stop looping animation!! 1 Answer

Unity 5: 2D Platformer: Trigger/Collisions problem 0 Answers

OnTriggerEnter triggers many times per trigger. 0 Answers

Trigger Collider is Triggering? 0 Answers

Trigger the Animation #2 after Animation #1 was triggered 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