- Home /
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:
Using Animation window
Using Particle System
Using scripts in update method (by increasing size and making it invisible gradually)
Please suggest the optimized method to do this.
@fafase @Harinezumi : I would be grateful to get help from you.....
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 :) )
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?
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.