Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Object fade out when close.

Discussion in 'Scripting' started by TheDiddyHop, May 11, 2014.

  1. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    I am working on a light effect for my game and would like it to fade out as you get closer. As you get further away however i would like it to fade back in. I have a basic script to make objects disappear when the player gets close but they dont come back. Everything else i know will likely cause the effect to clone itself when the player is far away from the effect causing a ton of lag. Can anyone help out with this?

    Here's my script at the moment though it is rather basic:
    Code (csharp):
    1. var Player : Transform ;
    2. var MinDist = 5;
    3.  
    4. function Start () {
    5.  
    6. }
    7.  
    8. function Update () {
    9.     if(Vector3.Distance(transform.position,Player.position) <= MinDist){
    10.     Destroy(gameObject);
    11. }
    12. }
    EDIT: Ok just thought that it would be useful to say that i'm using the Particle Additive shader.
     
    Last edited: May 11, 2014
  2. bara1247

    bara1247

    Joined:
    Oct 22, 2012
    Posts:
    40
    there are many ways to do it

    Code (csharp):
    1.  
    2.  if(Vector3.Distance(transform.position,Player.position) <= MinDist){
    3. gameObject.SetActive(false);
    4. }
    5. else{
    6. gameObject.SetActive(false);
    7. }
    8.  
    or

    Code (csharp):
    1.  
    2.  if(Vector3.Distance(transform.position,Player.position) <= MinDist){
    3. gameObject.renderer.enabled = false;
    4. }
    5. else{
    6. gameObject.renderer.enabled = false;
    7. }
    8.  
    or



    Code (csharp):
    1.  
    2.  
    3. var normalMat : Material
    4. var transpMat : Material
    5.  
    6. function Update()
    7. {
    8.  
    9.  if(Vector3.Distance(transform.position,Player.position) <= MinDist){
    10. gameObject.renderer.material = transpMat;
    11. }
    12. else{
    13. gameObject.renderer.material = normalMat;
    14. }
    15. }
    16.  
     
    Last edited: May 11, 2014
  3. TheDiddyHop

    TheDiddyHop

    Joined:
    Jul 26, 2013
    Posts:
    30
    Ok, the third script worked, however i need the effect to fade in. Do you have any idea how i could do this?
     
  4. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    The material will have to support transparency, of course.