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

logic help

Discussion in 'Scripting' started by San_Holo, Nov 10, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Hi, I've knackered my brains... need a tuna sandwich, anyhew, see the code below, It simple fades the alpha of a quad on update for a set time then repeats, it's a pulse fade effect from zero to one, but what should I create to make it pulse the other direction from 1 to zero in the update, I like the pulse effect but a more subtle effect is needed

    Code (csharp):
    1.         public GameObject Background;
    2.         private Material Background_Mat;
    3.         private float timer = 2.0f;
    4.    
    5.         void Start ()
    6.         {
    7.                 if (Background != null && Background.renderer != null) {
    8.                         Background_Mat = Background.renderer.material;
    9.                 }
    10.         }
    11.  
    12.         void Update ()
    13.         {
    14.                 timer -= Time.deltaTime;
    15.                 Color QuadAlpha = Background_Mat.color;
    16.                 QuadAlpha.a = (1.0f / timer);
    17.                 if (timer < 0.1f) {
    18.                         QuadAlpha.a = 1.0f;
    19.                         timer = 2.0f;
    20.                 }
    21.  
    22.                 Background_Mat.color = QuadAlpha;
    23.         }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Have you tried just using QuadAlpha.a = Mathf.PingPong(Time.time, 1.0f)
     
    San_Holo likes this.
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    That was just the ticket, thank you for letting me of of this, cheers to you.
     
  4. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    here's the simple script, just drag and drop a game object on there and your golden.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BackgroundPulse : MonoBehaviour
    6. {
    7.  
    8.         public GameObject Background;
    9.         private Material Background_Mat;
    10.    
    11.         void Start ()
    12.         {
    13.                 if (Background != null && Background.renderer != null) {
    14.                         Background_Mat = Background.renderer.material;
    15.                 }
    16.         }
    17.  
    18.         void Update ()
    19.         {
    20.                 Color QuadAlpha = Background_Mat.color;
    21.                 QuadAlpha.a = Mathf.PingPong (Time.time, 1.0f);
    22.                 Background_Mat.color = QuadAlpha;
    23.         }
    24. }