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

Audio Change pitch of music on death

Discussion in 'Audio & Video' started by zxzera, Jul 28, 2020.

  1. zxzera

    zxzera

    Joined:
    Jul 5, 2020
    Posts:
    4
    Hello. I am trying to change the pitch of the current audio clip playing on death but can't seem to get anywhere with it. I have this so far.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(AudioSource))]
    4.  
    5. public class DeathMusic : MonoBehaviour
    6. {
    7.     //private float startingPitch = 7;
    8.     private float deathPitch = 2.8f;
    9.     AudioSource audioSource;
    10.     private PlayerController playerControllerScript;
    11.     public AudioClip bgMusic;
    12.  
    13.     void Awake()
    14.     {
    15.         audioSource = GetComponent<AudioSource>();
    16.         audioSource.clip = bgMusic;
    17.         audioSource.Play();
    18.         audioSource.playOnAwake = true;
    19.         audioSource.loop = true;
    20.  
    21.         playerControllerScript = GetComponent<PlayerController>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (playerControllerScript.gameOver == true)
    28.         {
    29.             audioSource.pitch = deathPitch;
    30.         }
    31.     }
    32. }
    33.  
     
  2. zxzera

    zxzera

    Joined:
    Jul 5, 2020
    Posts:
    4
    Figured it out. I added in GameObject.Find("Player").. Works the way I want it to maybe not the most ideal code but

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(AudioSource))]
    6.  
    7. public class DeathMusic : MonoBehaviour
    8. {
    9.     private float startingPitch = 0.7f;
    10.     private float deathPitch = 0.28f;
    11.     AudioSource audioPitch;
    12.     private PlayerController playerControllerScript;
    13.     public AudioClip bgMusic;
    14.  
    15.     void Awake()
    16.     {
    17.         audioPitch = GetComponent<AudioSource>();
    18.         audioPitch.clip = bgMusic;
    19.         audioPitch.Play();
    20.         audioPitch.playOnAwake = true;
    21.         audioPitch.loop = true;
    22.         audioPitch.pitch = startingPitch;
    23.         playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (playerControllerScript.gameOver == true)
    29.         {
    30.             audioPitch.pitch = deathPitch;
    31.             Debug.Log("Changing pitch");
    32.         }
    33.     }
    34.  
    35.     /*void ChangePitch()
    36.     {
    37.  
    38.     }
    39.     private void OnCollisionEnter(Collision collision)
    40.     {
    41.  
    42.         if (playerControllerScript.gameOver == true)
    43.         {
    44.             audioPitch.pitch = deathPitch;
    45.             Debug.Log("Changing pitch");
    46.         }
    47.        
    48.     }*/
    49. }
    50.