7

In my game I want to make the player object transparent for 2 seconds by scripting at run time if the player collided with a specific object during the game ... is it possible ?

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Akari
  • 856
  • 8
  • 21
  • 36

4 Answers4

9

Check for collision. When the collision that you want is triggered then you can change the transparency.

GameObject g;

// 50% Transparency.
g.renderer.material.color.a = 0.5f; // a is the alpha value.

 // 100% Transparency.
g.renderer.material.color.a = 1.0f;

You can do just this to make your program wait time: http://docs.unity3d.com/Documentation/Manual/Coroutines.html

You will notice the example is exactly your question.

6

Try this extension method:

public static void ChangeAlpha(this Material mat, float alphaValue)
{
    Color oldColor = mat.color;
    Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaValue);         
    mat.SetColor("_Color", newColor);               
}

You can then call it by:

gameObject.renderer.material.ChangeAlpha( Your Alpha Value );
Community
  • 1
  • 1
MichaelTaylor3D
  • 1,615
  • 3
  • 18
  • 32
1

In Unity 5, the best way (TO MAKE AN OBJECT INVISIBLE) that worked for me was to:

  1. Set all of the game object's materials you want to be invisible to transparent under the rendering mode.
  2. Then, click on the small round button next to albedo and scroll down on the list of items given until you find one called UIMask.
  3. Highlight it and press enter.

* Note that this is a hard fix and I'm not sure if you can change this with code.

** This was made for boundaries in roll-a-ball with a player jump function included. I needed to make the walls invisible but also collision-able to stop the air born player object.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    that can mess up if other objects use that object, its best to create a new shader to do this and turn off all of the extras to the mesh renderer. – 1-14x0r Nov 10 '20 at 01:09
0

This can be simply done with shaders, which you can quickly and efficiently change at runtime:

  • Create new material and set the shader to use the
    Unlit/Transparent Cutout
    
  • Assign your game object to use this new material
  • Turn off all of the stuff in the mesh render of the game object: set cast shadows to off, receive shadows disabled, contribute to global illumination to disabled, turn off your light probes and reflections, set motion vectors to 'force no motion', turn off dynamic occlusion.
  • Also don't forget to remove the mesh collider of the game object.
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
1-14x0r
  • 1,697
  • 1
  • 16
  • 19