Hi, I’m new to using both Unity and FMOD, I’m a sound designer and iI want to expand my skills in sound integration in games.
Long story short, I was following the tutorial on the Survival Shooter (Audio for Unity 5: Survival Shooter (1/5) - Up and Running - YouTube) and I got stuck in the moment where I have to set the Parameter Health in FMOD based on the currentHealth variable in the script.
Not only I can’t manage to set the parameter value, but I can’t trigger the event either.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
using FMOD.Studio;
using System;
namespace CompleteProject
{
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100; // The amount of health the player starts the game with.
public int currentHealth; // The current health the player has.
public Slider healthSlider; // Reference to the UI's health bar.
public Image damageImage; // Reference to an image to flash on the screen on being hurt.
public float flashSpeed = 5f; // The speed the damageImage will fade at.
public Color flashColour = new Color(1f, 0f, 0f, 0.1f); // The colour the damageImage is set to, to flash.
Animator anim; // Reference to the Animator component.
PlayerMovement playerMovement; // Reference to the player's movement.
PlayerShooting playerShooting; // Reference to the PlayerShooting script.
bool isDead; // Whether the player is dead.
bool damaged; // True when the player gets damaged.
bool playerStateStartCheck;
// set instance variables
public string PlayerStateEvent = "event:/SFX/Player/Player_Heartbeat";
FMOD.Studio.EventInstance playerState;
FMOD.Studio.PARAMETER_ID healthParameterId;
void Awake ()
{
// Setting up the references.
anim = GetComponent <Animator> ();
playerMovement = GetComponent <PlayerMovement> ();
playerShooting = GetComponentInChildren <PlayerShooting> ();
// Set the initial health of the player.
currentHealth = startingHealth;
playerStateStartCheck = false;
}
void Start()
{
// initialise instances
playerState = FMODUnity.RuntimeManager.CreateInstance(PlayerStateEvent);
FMOD.Studio.EventDescription healthEventDescription;
playerState.getDescription(out healthEventDescription);
FMOD.Studio.PARAMETER_DESCRIPTION healthParameterDescription;
healthEventDescription.getParameterDescriptionByName("Health", out healthParameterDescription);
healthParameterId = healthParameterDescription.id;
}
void Update ()
{
// If the player has just been damaged...
if(damaged)
{
// ... set the colour of the damageImage to the flash colour.
damageImage.color = flashColour;
playerState.setParameterByID(healthParameterId, (float)currentHealth);
}
// Otherwise...
else
{
// ... transition the colour back to clear.
damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
//CALL HEARTBEAT
if (currentHealth <= 50 && !isDead && !playerStateStartCheck)
{
//FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Player/Player_Heartbeat", GetComponent<Transform>().position);
playerState.start();
playerStateStartCheck = true;
}
// Reset the damaged flag.
damaged = false;
}
public void TakeDamage (int amount)
{
// Set the damaged flag so the screen will flash.
damaged = true;
// Reduce the current health by the damage amount.
currentHealth -= amount;
// Set the health bar's value to the current health.
healthSlider.value = currentHealth;
// Play the hurt sound effect. FMOD HERE
FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Player/Player_Hurt", GetComponent<Transform>().position);
playerState.setParameterByName("Health", currentHealth);
// If the player has lost all it's health and the death flag hasn't been set yet...
if (currentHealth <= 0 && !isDead)
{
// ... it should die.
Death ();
}
}
void Death ()
{
// Set the death flag so this function won't be called again.
isDead = true;
playerState.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
// Turn off any remaining shooting effects.
playerShooting.DisableEffects ();
// Tell the animator that the player is dead.
anim.SetTrigger ("Die");
// Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing). FMOD HERE
FMODUnity.RuntimeManager.PlayOneShot("event:/SFX/Player/Player_Death", GetComponent<Transform>().position);
// Turn off the movement and shooting scripts.
playerMovement.enabled = false;
playerShooting.enabled = false;
}
public void RestartLevel ()
{
// Reload the level that is currently loaded.
SceneManager.LoadScene (0);
}
}
}