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 lagidigu3ll · Feb 04, 2015 at 09:58 AM · animation2dcollisiontriggercoin

How to handle animation of multiple Coins?

Hi There!

I've been working on a 2d game, where the Character needs to pick up coins, and when a coin is picked up, it should make a simple animation in terms of movement, rotation and scale.

It looks like this: (The script is attached to the character)

 using UnityEngine;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour {
 
     bool triggerAnimation = false;
     int triggerTimer;
     GameObject Coin;
 
     void Start () {
         triggerTimer = 300;
     }
 
     void FixedUpdate () {
         if (triggerAnimation && triggerTimer < 29) {
             if (Coin != null) {
                 Coin.transform.Translate (0, 0.1f, 0, Space.World);
                 Coin.transform.Rotate (0, 0, 20, Space.World);
                 Coin.transform.localScale /= 1.05f;
             }
         }
         triggerTimer++;
     }
 
     void OnTriggerEnter2D(Collider2D trigger) {
         if (trigger.gameObject.tag == "coinSimple") {
             Coin = trigger.gameObject;
             triggerAnimation = true;
             triggerTimer = 0;
             Destroy(trigger.gameObject, 0.3f);
         }
     }
 }
 

Thing is, as expected, only one coin at a time can have an animation, when another coin gets picked up, the previous coin stop its animation midway.

I'm reluctant to make a script for every coin, as there will be tons of them in the scene, and I fear it might have a heavy effect on performance.

I know why there is this problem, but I don't know how to fix it, is there any feature I can use that will prevent this?

Thanks a bunch in advance :D

Comment
Add comment · Show 3
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 kevinspawner · Feb 04, 2015 at 10:18 AM 0
Share

Why not add a new script with animation and attach it to the coin prefab. So whenever the coin prefab is called the script will play as it is attached to the coin.

avatar image AngryBurritoCoder · Feb 04, 2015 at 11:37 AM 0
Share

I agree with kevinspawner, make a method on the coin script with the animation and destroy part . Then when player collides with any coin, it gets that coin script component and runs the method on that coin and the coin does all the job, this should be pretty efficient way of doing this all.

avatar image lagidigu3ll · Feb 04, 2015 at 12:08 PM 0
Share

Thanks a lot! :D

1 Reply

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

Answer by Ferb · Feb 04, 2015 at 10:26 AM

One solution would be to make a Mecanim animation clip for the coin being collected, and just activate that when the coin is touched. I don't know what is the effect on performance of all the coins being animated (the animator for each coin would be running even when the coin wasn't being picked up), but animators have a setting only to run when the object is currently visible, which would help I suppose. Animate Anything With Mecanim

Another solution would be to turn your variables (triggerAnimation, triggerTimer, Coin) into Lists, so that each one could values for a bunch of coins. That way, instead of FixedUpdate running that code for one coin, it would see how many are in the list, and run it for each one. MSDN List Class Documentation

If you've used lists before, you may as well use that. Otherwise, it's probably best to use Mecanim - it will be simpler, and will allow you to easily add other animations like making them spin before they get picked up.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make animation transitions a one-way street? 1 Answer

Trying to animate on collision, Help Please. 0 Answers

2D Triggers and Collisions not working as expected. 2 Answers

How to use Physics2D.IgnoreCollision2D in Unity 5 to cancel a trigger 0 Answers

How to detect in grid of objects if one is destroyed while beside adjacent objects without rigidbody? 1 Answer


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