Answered
0
0

Hi!
I’m struggling to find a solution to setup correctly a script to control the parameter of my music event in FMOD.

What I want to do is very simple: I have a looping track that should transition to an ending when the game is over (i.e. the player health is =< 0). The event is named “BackgroundMusic” ad I have a “currentHealth” parameter set up with min-max values 0.00 – 100.00 (max health in game). A transition region to the ending is set up to be active under a parameter = 0.00 condition.

Now, to do this in Unity, I’m not sure how to control the parameter with the script.
I tried to use a script similar to the one described in the FMOD Unity integration documentation with no success.
I attached this code to an object in the game (the game has a PlayerHealth script):

using UnityEngine;
using System.Collections;

public class MusicManager : MonoBehaviour 
{
    public PlayerHealth playerHealth; 
    FMOD.Studio.EventInstance backgroundMusic;
    FMOD.Studio.ParameterInstance currentHealth;



    // Use this for initialization
    void Start () 
    {
        backgroundMusic = FMOD_StudioSystem.instance.GetEvent ("event:/Music/BackgroundMusic");
        backgroundMusic.start ();
        backgroundMusic.getParameter (“xxx", out xxx);
    }

    // Update is called once per frame
    void Update () 
    {
        currentHealth.setValue (playerHealth.currentHealth);
    }
}

I don’t know what to fill the xxx with, trying out different things the only thing I managed to achieve was for the music to play from the ending (i.e. FMOD doesn’t receive the parameter update and set it to 0 by default).
As you may have noticed, I’m not a programmer and I’m really new to this.
How do I get the player health information to feed FMOD so that I can control the parameters?

The other thing I thought was that, rather than attach it to an object, I could implement the music right in the PlayerHealth script, but all my attempts lead to a failure.

Here’s the PlayerHealth script (the other FMOD objects you see in this are other player sounds that I manage to implement correctly):

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerHealth : MonoBehaviour
{
    public int startingHealth = 100;
    public int currentHealth;
    public Slider healthSlider;
    public Image damageImage;
    public float flashSpeed = 5f;
    public Color flashColour = new Color(1f, 0f, 0f, 0.1f);


    Animator anim;
    PlayerMovement playerMovement;
    PlayerShooting playerShooting;
    bool isDead;
    bool damaged;

    void Awake ()
    {
        anim = GetComponent <Animator> ();
        playerMovement = GetComponent <PlayerMovement> ();
        playerShooting = GetComponentInChildren <PlayerShooting> ();
        currentHealth = startingHealth;
    }


    void Update ()
    {
        if(damaged)
        {
            damageImage.color = flashColour;
        }
        else
        {
            damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
        }
        damaged = false;
    }


    public void TakeDamage (int amount)
    {
        damaged = true;

        currentHealth -= amount;

        healthSlider.value = currentHealth;

        FMOD_StudioSystem.instance.PlayOneShot ("event:/Player Sounds/Player Hurt", new Vector3(0,0,0));

        if(currentHealth <= 0 && !isDead)
        {
            Death ();
        }
    }


    void Death ()
    {
        isDead = true;

        playerShooting.DisableEffects ();
        anim.SetTrigger ("Die");
        FMOD_StudioSystem.instance.PlayOneShot ("event:/Player Sounds/Player Death", new Vector3(0,0,0));

        playerMovement.enabled = false;
        playerShooting.enabled = false;
    }


    public void RestartLevel ()
    {
        Application.LoadLevel (Application.loadedLevel);
    }
}

If anyone could give me an help that would be greatly appreciated!

  • You must to post comments
Best Answer
0
0

The xxx should be currentHealth. Your scripts look good at first glance. I would suggest enabling Live Update in Unity and connect the profiler and checking if the parameter values are coming through.

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.