zxzera
1
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.
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class DeathMusic : MonoBehaviour
{
//private float startingPitch = 7;
private float deathPitch = 2.8f;
AudioSource audioSource;
private PlayerController playerControllerScript;
public AudioClip bgMusic;
void Awake()
{
audioSource = GetComponent<AudioSource>();
audioSource.clip = bgMusic;
audioSource.Play();
audioSource.playOnAwake = true;
audioSource.loop = true;
playerControllerScript = GetComponent<PlayerController>();
}
// Update is called once per frame
void Update()
{
if (playerControllerScript.gameOver == true)
{
audioSource.pitch = deathPitch;
}
}
}
zxzera
2
Figured it out. I added in GameObject.Find(“Player”)… Works the way I want it to maybe not the most ideal code but
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class DeathMusic : MonoBehaviour
{
private float startingPitch = 0.7f;
private float deathPitch = 0.28f;
AudioSource audioPitch;
private PlayerController playerControllerScript;
public AudioClip bgMusic;
void Awake()
{
audioPitch = GetComponent<AudioSource>();
audioPitch.clip = bgMusic;
audioPitch.Play();
audioPitch.playOnAwake = true;
audioPitch.loop = true;
audioPitch.pitch = startingPitch;
playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
}
void Update()
{
if (playerControllerScript.gameOver == true)
{
audioPitch.pitch = deathPitch;
Debug.Log("Changing pitch");
}
}
/*void ChangePitch()
{
}
private void OnCollisionEnter(Collision collision)
{
if (playerControllerScript.gameOver == true)
{
audioPitch.pitch = deathPitch;
Debug.Log("Changing pitch");
}
}*/
}