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.

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 :stuck_out_tongue: ), 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 :slight_smile: )