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
0
Question by raabaa · Jan 29, 2018 at 06:43 AM · animationparticle system

How to expand and fade game objects in Unity?

I want to expand fade 3d object after something collides it. I am beginner to unity and game development. As per my understanding we can do it in 3 ways:

  1. Using Animation window

  2. Using Particle System

  3. Using scripts in update method (by increasing size and making it invisible gradually)

Please suggest the optimized method to do this.

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 raabaa · Jan 29, 2018 at 07:33 AM 0
Share

@fafase @Harinezumi : I would be grateful to get help from you.....

1 Reply

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

Answer by Harinezumi · Jan 29, 2018 at 09:04 AM

Hi raabaa,
I think the solution depends on what you are trying to do. Do I understand it correctly that you want your object after collision to 1) stop interacting 2) fade out over a time and 3) expand over time?
If this is the case, animating is certainly a possibility (but I am a programmer, so I prefer code :P ), but I don't know how to do it with a particle system.
Doing it with animation you just add an Animation (which is not the same as an Animator) component, create a new AnimationClip, add the properties Transform.scale, and Renderer.material.color (your material will need to be Fade type!), then at the final key position set the size (for all 3 components separately!) and the final color value. Then you have to play this animation from code in your collision detection, usually it is as easy as animator.Play() or animator.Play("<name of your animation>");

With code it is a bit simpler (for me): you declare variables for your transition duration and final size, then start a Coroutine that uses lerp:

 [SerializeField] private float fadeOutDuration = 1f; // fades out in 1 second
 [SerializeField] private float fadeOutSizeMultiplier = 2f; // doubles the size by the end
 
 // call this when you want to expand-fade
 private HandleCollision() {
     ownCollider.enabled = false; // disable your Collider to disable interaction with world; I assume you get it in Awake()
     ownRigidbody.isKinematic = true; // "disable" Rigidbody as well; again, get it in Awake()
     StartCoroutine(FadeOutExpand());
 }
 
 private IEnumerator FadeOutExpand() {
     // store original color and scale to be able to correctly scale
     Color originalColor = ownRenderer.material.color; // I assume you get ownRenderer in Awake()
     Vector3 originalScale = transform.scale;

     float t = 0;
     while (t < fadeOutDuration) {
         SetAlphaAndScale(originalColor, originalScale, t / fadeOutDuration); // passes value between 0 and 1
         yield return null;
         t += Time.deltaTime;
     }
     SetAlphaAndScale(1); // make sure you get the correct final size and color even with imprecise floating point and time
 }
 
 private void SetAlphaAndScale (Color originalColor, Vector3 originalScale, float t) {
     ownRenderer.material.color = new Color(originalColor.r, originalColor.g, originalColor.b, Mathf.Lerp(1, 0, t)); // assign a new color with decreasing alpha
     transform.scale = orignalScale * Mathf.Lerp(1, fadeOutSizeMultiplier, t); // scale relative to original size
 }

Additionally, you might want to look into different interpolations, like Sinerp() and Hermite() which create smoother transitions at the end - see the extremely useful Mathfx from Unity Wiki!

(Btw, I feel honored for being personally called to help :) )

Comment
Add comment · Show 2 · 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 raabaa · Jan 29, 2018 at 11:12 AM 0
Share

Thanks for the wonderful answer. I tried the code and it works. However, if i apply the code in the pooled object then the newly instantiated objects get affected(they appear enlarged). Is there any specific mechanism to avoid this?

avatar image Harinezumi raabaa · Jan 29, 2018 at 11:37 AM 0
Share

If you use object pooling, you will need to reset an object some way before you reuse it. For example, you could create an abstract base class Poolable : $$anonymous$$onoBehaviour and declare public abstract void Reset();. Then your objects in the pool would need to have a component on them that is an implementation of the Poolable, and the pool would store references to Poolable objects. Finally, just before you reuse an object from the pool, you call Reset() on it. Reset() can have any custom logic you want, just don't make it too complicated or the pool loses its point.

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

201 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

Related Questions

Can I make animations snap to a frame? 1 Answer

How do I move an object from A to B, then another object from A to C? 1 Answer

Mesh Particle Emitter & Animation 0 Answers

How to select an animation clip by index number? 7 Answers

how can i make my particle system follow a game object without having the particles preform the game object animations 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