Fade out an object before destroying it

Hi, is there a way to make an object fade to nothing before destroying it? It would look better, rather than having it just disappear.

1 Like

Could you not change the alpha section of the object material(s) over time? You would need to change your shaders to transparent versions so I’m not sure how they would look etc. Worth doing a test or two me thinks.

You could then have a quick check, if alpha <= 0 destroy the object.

Regards,
Matt.

You could use this framework for that exact thing: http://forum.unity3d.com/viewtopic.php?t=47351&postdays=0&postorder=asc&start=0

I think I will use alphas, but how would I do that? I dont change textures much, so IDK how I would do it…

Any one? But what is an alpha anyway? Would i use:renderer.material.a = 0 Or something like that? And doesnt changing the alpha on 1 material cause all the objects in the scene to become transparent too?

1 Like

Make sure the material’s shader allows transparency (say, the Transparent/Diffuse shader). Every material has a color parameter; change the alpha value of it to 0 for full transparency.

renderer.material.color.a = 0;

For the fade effect, I recommend using the Animation editor to animate the alpha parameter; no additional framework required. Simply trigger this animation and wait for its completion before destroying the object.

No. The material attached to a game object is an instance of that material. Just like you can change the parameters of a script and other objects with that same script attached remain unaffected.

1 Like

iTween can do th e fade effect with one line of code i belive. i plan to use it for the same effect.

You are correct, then add an onComplete callback that destroys the object. :slight_smile:

Here’s how with iTween:

function Start () {
	iTween.fadeTo(gameObject,{"alpha":0, "onComplete":"destroy"});
}

function destroy():void{
	Destroy(gameObject);
}
1 Like

But how would I make the alpha so I can animate it anyway?

Every Color has an alpha value already, no need to “make” it. As I mentioned above, set your material’s shader to Transparent/Diffuse (or some variation) and animate the alpha value of the material’s colour however you please.

Update v1.0.14:

  • Changed punchPosition and shake to use localPosition instead of worldPosition
  • Fixed shake’s callback routine
  • Added “loopType” argument for most methods (check the documentation for which ones now have it) with the ability to set it as “loop” or “pingPong” (there’s issues with the moveToBezier and moveToBezierWorld that I “may” get to)
  • Added new sample project to support page

UPDATE:

*JavaScript version of iTween is now 1.0.15

*I threw together a simple game with source code for everyone on the support site to help show some uses of iTween in an actual project.

*New video on the support site that goes through EVERYTHING that iTween can currently do for you!

http://www.pixelplacement.com/iTween/

What about using Mathf.Lerp(1,0,10);? Well, anyway, how would I change the alpha on all the children’s textures as well?

1 Like

I guess you’d have to add them one by one to an iTween.fade. Yeah?

Update: v 1.0.17

For those who prefer using native data types instead of separating out individual properties (“x”,“y”, etc…) I’ve added a ton of new properties that leverage Vector3 and such - check the documentation for details.

Added “position”:Vector3 to moveTo, moveToWorld, moveFrom, moveFromWorld

Added “amount”:Vector3 to moveBy, moveByWorld

Added “color”:Color to colorTo, colorFrom

Added “scale”:Vector3 to scaleTo, scaleFrom

Added “amount”:Vector3 to scaleBy, scaleAdd

Added “rotation”:Vector3 to rotateTo, rotateFrom

Added “amount”:Vector3 to rotateBy

Added “position”:Vector3 to punchPosition

Added “rotation”:Vector3 to punchRotation

Added “amount”:Vector3 to shake

Examples:

iTween.colorFrom(gameObject,{"color":Color.yellow});
iTween.colorTo(gameObject,{"r":2,"delay":3});
iTween.moveFrom(gameObject,{"position":Vector3(-1,-1,-1)});
iTween.moveFromWorld(gameObject,{"position":Vector3(-1,-1,-1)});
iTween.moveTo(gameObject,{"x":2,"delay":2});
iTween.moveTo(gameObject,{"position":Vector3(0,0,0),"delay":4});
iTween.scaleTo(gameObject,{"scale":Vector3(2,2,2)});
iTween.scaleFrom(gameObject,{"scale":Vector3(2,2,2)});
iTween.moveByWorld(gameObject,{"amount":Vector3(2,2,2)});
iTween.rotateTo(gameObject,{"rotation":Vector3(12,12,12)});
iTween.rotateFrom(gameObject,{"rotation":Vector3(12,12,12)});
iTween.scaleAdd(gameObject,{"amount":Vector3(2,2,2)});
iTween.scaleBy(gameObject,{"amount":Vector3(2,2,2)});
iTween.rotateBy(gameObject,{"amount":Vector3(.5,0,0)});
iTween.punchPosition(gameObject,{"position":Vector3(1,0,0)});
iTween.punchRotation(gameObject,{"rotation":Vector3(.1,0,0)});
iTween.shake(gameObject,{"amount":Vector3(.2,0,0)});
1 Like

edplane:
I’ll add an option to fadeTo and fadeFrom called ‘includeChildren’:Boolean that will be true by default and it will automatically fade all children. Cool??

edplane:

Version 1.0.18 is now live and the color and fade methods automatically effect any children objects (you can toggle this behavior with “includeChildren”).

Cool?

UPDATE JS v 1.0.20

  • Smoothed out orientToPath controls for bezier movements
  • Fixed some slight issues with local and world space with bezier movements
  • moveToBezier (only using local) with orientToPath doesn’t work for correct lookAt on contained objects (objects that have a parent) - ANY SUGGESTIONS?
  • Deleted moveByWorld since it wasn’t logical (if this is in error, let me know)
  • moveFrom wasn’t calculating correctly between local and world space - does now
  • Fixed pingPong loop failures throughout
  • Added rotateAdd
  • Rotational math for local usage is still odd - ANY SUGGESTIONS?
  • Fixed pingPong loops on bezier movements
  • Added loopType to scaleBy, scaleAdd, rotateBy, rotateAdd

UPDATE: JavaScript v 1.0.23

This version ushers in the first of many new ‘Continuously Callable’ methods. That’s right boye and girls, no more hunting around for smooth camera controllers and the like! You can easily cause iTween to act as a smooth camera follow script by the first offering called lookToUpdate:

var target : Transform;

function Update () {
	iTween.lookToUpdate(gameObject,{"target":target.position, "lookSpeed": 3}a;
}

Just released a sizable update to the JavaScript version, can you guys let me know if any new bugs crept up!

1.0.28 JavaScript iTween
Fixed:
-All “By” and “Add” move methods can now take “position” or “amount”

-All “By” and “Add” rotate methods can now take “position” or “amount”

-Every “By” and “Add” method now snapshots it’s start position when it is run rather than when its initialized to avoid odd behaviours when delays ae utilized

-Changed lookFrom to use startRotation not endRotation

-Added moveAdd to enhance flexibility

-Cleaned up and optimized all move methods to be more predictable and reliable (utilizing Translate for better local space movement)

-Cleaned up and optimized all rotate methods to be more predictable and reliable (utilizing Rotete for better local space movement)

-Cleaned up and optimized punchPosition and punchRotation to be more predictable and reliable (utilizing Translate for better local space movement)

-Cleaned up and optimized all scale methods to be more predictable and reliable

-Added lookToUpdateWorld and corrected world usage for lookToUpdate

-Corrected issue with bezier move and orientToPath failing if the object was a child

-Bezier move now uses lookToUpdateWorld regardless of situation (I “think” this is the correct solution)

-Added moveToUpdateWorld

-Added axis argument to bezier move to allow locking of look application to single axis

-Bezier move lookAt now uses lookToUpdateWorld

-Added lookToWorld

-MoveBy and MoveAdd are now identical in operation

-Added shakeWorld

-Shake methods not correctly resolve magnitude to 0

-Consolidated rotateToDefaults and rotateByDefaults into rotateDefaul