Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do i make this fade out?

Discussion in 'Scripting' started by vitamiin, Jan 17, 2022.

  1. vitamiin

    vitamiin

    Joined:
    Sep 16, 2021
    Posts:
    3
    1. IEnumerator FadeInSound(){
    2. float SecondsToFade = 1.5f;
    3. float startVol = walkingSound.volume;
    4. float rate = 1.0f / SecondsToFade;

    5. for (float x = 0.0f; x <= 1.0f; x += Time.deltaTime * rate) {
    6. walkingSound.volume = Mathf.Lerp(startVol, targetVol, x);
    7. yield return null;
    8. }
    9. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Fading, simple and easy:

    https://forum.unity.com/threads/how...color-with-duration-time.969864/#post-6311295

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     
    Last edited: Jan 17, 2022
  3. vitamiin

    vitamiin

    Joined:
    Sep 16, 2021
    Posts:
    3
    Why is my code not fading and why is it laggy?

    [CODE/using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MediaPlayer : MonoBehaviour
    {
    [SerializeField] GameObject[] audioObjects;
    [SerializeField] float fadeVolume = 1f;
    [SerializeField] float targetVolume = 1f;

    bool[] isPlaying = new bool[13];

    private void Update()
    {
    fadeVolume = Mathf.MoveTowards(fadeVolume, targetVolume, 5.0f * Time.deltaTime);
    }

    public void DrumPlay()
    {
    if (!isPlaying[0])
    {
    targetVolume = 1f;
    audioObjects[0].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[0] = true;

    }
    else
    {
    targetVolume = 0f;
    audioObjects[0].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[0] = false;

    }

    }

    public void BirdPlay()
    {
    if (!isPlaying[1])
    {
    targetVolume = 1f;
    audioObjects[1].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[1] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[1].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[1] = false;
    }

    }

    public void HardWindWithPlingPlay()
    {
    if (!isPlaying[2])
    {
    targetVolume = 1f;
    audioObjects[2].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[2] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[2].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[2] = false;
    }

    }

    public void HeavyRainPlay()
    {
    if (!isPlaying[3])
    {
    targetVolume = 1f;
    audioObjects[3].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[3] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[3].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[3] = false;
    }

    }

    public void LightRainPlay()
    {
    if (!isPlaying[4])
    {
    targetVolume = 1f;
    audioObjects[4].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[4] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[4].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[4] = false;
    }

    }

    public void SonarPlay()
    {
    if (!isPlaying[5])
    {
    targetVolume = 1f;
    audioObjects[5].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[5] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[5].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[5] = false;
    }

    }

    public void MunkSongPlay()
    {
    if (!isPlaying[6])
    {
    targetVolume = 1f;
    audioObjects[6].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[6] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[6].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[6] = false;
    }

    }

    public void HighPitchPlingPlay()
    {
    if (!isPlaying[7])
    {
    targetVolume = 1f;
    audioObjects[7].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[7] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[7].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[7] = false;
    }

    }

    public void ArpPlay()
    {
    if (!isPlaying[8])
    {
    targetVolume = 1f;
    audioObjects[8].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[8] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[8].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[8] = false;
    }

    }

    public void PadPlay()
    {
    if (!isPlaying[9])
    {
    targetVolume = 1f;
    audioObjects[9].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[9] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[9].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[9] = false;
    }

    }

    public void SubPlay()
    {
    if (!isPlaying[10])
    {
    targetVolume = 1f;
    audioObjects[10].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[10] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[10].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[10] = false;
    }

    }

    public void TopBassPlay()
    {
    if (!isPlaying[11])
    {
    targetVolume = 1f;
    audioObjects[11].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[11] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[11].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[11] = false;
    }

    }

    public void TickTackPlay()
    {
    if (!isPlaying[12])
    {
    targetVolume = 1f;
    audioObjects[12].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[12] = true;
    }
    else
    {
    targetVolume = 0f;
    audioObjects[12].GetComponent<AudioSource>().volume = fadeVolume;
    isPlaying[12] = false;
    }

    }

    } Code]
     
    Last edited: Jan 17, 2022
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Boz0r likes this.