Leçon 8

Table des matières

Creating Diversity Using Game Parameters

While you can use States and Switches to change between musical conditions, like which region you are in, you can also use Game Parameters to influence minor details in the music, like a harp playing louder the closer you get to the waterfall. You can map almost any in-game feature to a Game Parameter, and then choose which Music Track or Effects should be influenced by it.

With Wwise, you already have a few built-in Game Parameters, like Distance to the player, but these Game Parameters are often more ideal for diegetic sounds (positioned in a space), whereas Music is more often non-diegetic (outside the game space). One way to integrate a Game Parameter directly into elements in the environment is to write a custom script and map the Game Parameter to a feature in the game. This will allow you to use positioning information from one object in the game to inform a non-positioned sound located on another game object.

In the following steps, you will create and add a script to the Waterfall. In this script, you will create a Wwise-Type RTPC property and give it the distance from the Player to the Waterfall. You will then assign the Game Parameter to the Woodlands theme's harp Music Track, making it increase or decrease in volume based on distance.

[Note]

Do not use the Distance_to_Player Game Parameter in Wwise on the Woodlands 1_Harp Music Track because the Music_Region Event is not posted on the Waterfall_UpClose game object.

  1. In the Unity menu, go to Audiokinetic > Certification > 301 > Lesson 8 and select Creating Diversity using Game Parameters.

    The Waterfall game object is fairly large and its pivot point is quite some distance outside the walkable map, so by measuring the distance from the actual Waterfall to the Player would not be good practice.

    Instead, a child game object has been created and placed right next to the Waterfall pond (Waterfall Up Close), which enables you to very precisely control the point from which the distance is measured.

    [Tip]

    You will be calculating the distance from the Pivot Point (center of the game object) to the Player, as this position can easily be retrieved from the Transform component.

  2. In the Hierarchy, expand the Waterfall game object and select the Waterfall_UpClose game object.

  3. In the Inspector, click Add Component, go to New script, name it SetRTPCToDistance, select Create and Add and open the script.

    Let's start by cleaning up the script. You are not going to use the Start() function as you will be setting the Game Parameter repeatedly, so let's remove it.

  4. Delete the Start(){} function and the comment // Use this for initialization above it.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
    
    public class SetRTPCToDistance : MonoBehaviour {
    
        // Update is called once per frame.
        void Update () {
    
        }
    }

    Let's first make a public Wwise-Type RTPC property and name it GameParameter.

  5. Inside the SetRTPCToDistance class, type public AK.Wwise.RTPC GameParameter;.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
    
    public class SetRTPCToDistance : MonoBehaviour {
        public AK.Wwise.RTPC GameParameter;
    
        // Update is called once per frame.
        void Update () {
    
        }
    }

    Before you can calculate a distance from anything to this game object, you need the reference to another game object's Transform component. As we've seen in previous lessons, the Transform component contains information about the game object's position, scale, and rotation, allowing Unity to place every game object in relation to the Scene's center point with the right size and orientation. For keeping track of positions, the Transform component has a Vector3 property called Position which we will use to monitor the distance between the Player and Waterfall.

    [Note]

    Vector3 is a property type containing numbers with decimals. With positioning, Vector3 describes the X, Y, and Z coordinates.

    Good practice is to make your scripts as generic as possible, so you can reuse the script instead of making another. With this in mind, let's create a public property of the type Transform. Since this script is running on the Waterfall_UpClose game object, you will want to reference the Player game object in that property when calculating the distance. You should also give it an appropriate name that describes what it is, like OtherTransform.

  6. Type public Transform OtherTransform;.

    public class SetRTPCToDistance : MonoBehaviour {
        public AK.Wwise.RTPC GameParameter;
        public Transform OtherTransform;

    With the RTPC and Transform properties, you can now start calculating the distance between the Player and Waterfall_UpClose Transforms and set the Game Parameter to that distance.

    Using the Transform's Position properties from two different game objects, you can calculate the distance between them using the Distance() function. This call will have to be set in the Update() function, so that the distance is constantly updated.

  7. In the Update() function, type Vector3.Distance();.

    // Update is called once per frame.
    void Update () {
        Vector3.Distance();
    }

    The Vector3.Distance() function will need the position of each game object's transform. To retrieve the Vector3 Position property, you call the Transform component.

  8. In the Vector3.Distance() function brackets, type OtherTransform.position.

    void Update () {
        Vector3.Distance(OtherTransform.position);
    }

    Now we have to reference the transform on the Waterfall_UpClose game object. To send another Vector3 property into the function, just add a comma ( ,) and write the next property.

  9. Continued on OtherTransform.position, add a comma (,) and type transform.position.

    Vector3.Distance(OtherTransform.position, transform.position); 
     

    You are now calculating the distance, but you are not saving the result. To save it, you need to set it into a float variable. Using the float variable type is important because it will allow you to store values with decimals, ensuring a fluid RTPC transition, whereas variable types like int or uint can only contain non-decimal numbers. You can now save the returned result from the Vector3.Distance() function in a float variable by writing its name ("distance"), an equality operator (=), and the Vector3.Distance() function.

  10. In front of the Vector3.Distance() function, type float distance =.

    float distance = Vector3.Distance(OtherTransform.position, transform.position); 
     

    [Tip]

    To further optimize this script, you could move the creation of your variable 'float distance' outside of the Update function and then only set the distance variable in Update. By declaring this variable before the Update function, this would avoid creating a new variable every frame.

    Next, you should map the game parameter to the distance value. To influence the music, which is being posted on a different game object, you should use the .SetGlobalValue() function.

  11. Add a new line below the float distance variable and type GameParameter.SetGlobalValue(distance);.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class SetRTPCToDistance : MonoBehaviour {
        public AK.Wwise.RTPC GameParameter;
        public Transform OtherTransform;
    
        // Update is called once per frame.
        void Update () {
            float distance = Vector3.Distance(OtherTransform.position, transform.position);
            GameParameter.SetGlobalValue(distance);
        }
    }

    That's all the code you need to write. Next, you need to assign the properties in the Inspector.

  12. In the Hierarchy, make sure the Waterfall_UpClose game object is selected.

  13. In the Unity Inspector, go to the SetRTPCToDistance's Game Parameter property, then expand Game Parameters > Gameplay and select Music_WoodlandsHarp.

    In the OtherTransform property, you can add the game object's Transform component that the script should calculate distance to. In this case, you will be using the Player game object.

  14. From the Hierarchy, drag the Player game object into the OtherTransform property.

    Before entering Play mode, let's also configure the Game Object Profiler to monitor the Game Parameter and connect to the game.

  15. In Wwise, go to Layouts and select Game Object Profiler.

  16. In the game Sync Monitor, click Add…

  17. In the Project Explorer - Browser, expand Gameplay and double-click the Music_WoodlandsHarp.

    You have now selected the Game Parameter and should add the game object. As the Music_WoodlandsHarp is only used at the Waterfall_Up_Close game object, you can simply monitor the Game Parameter in a Global scope.

  18. In the Game Object Explorer's Watches tab, open the Selector and choose Global Game Object.

    The Game Object Profiler is now ready.

  19. Connect to the game and enter Play mode. Run to the Waterfall in the Woodlands.

    As you get closer to the Waterfall, pay attention to the harp in the Woodlands theme. You may exit Play mode.

Did you notice that the Woodlands theme's harp would increase in volume as you got closer? Let's take a look in Wwise.

In the Game Sync Monitor you will see that the Music_WoodlandsHarp Game Parameter on the Waterfall_UpClose game object would decrease in value. This value refers to the distance, meaning that you are having the expected behavior by approaching the Waterfall. As such, with just a few lines of code, you can make your music composition adapt to what the player chooses to do, and if you write the script with reusability in mind, you can quickly add it to many different objects in your Scene.

[Tip]

You might already have made the connection, but another example of using Game Parameters to control music tracks is the enemy music, described earlier in this lesson. For each type of enemy, there's a Game Parameter and, depending on distance, the corresponding music layers will increase or decrease in volume. The enemy music Game Parameters system is more complex and integrated directly into the GameManager script, but if you want to take a look, search for 'GameManager' in the Project view, open the GameManager.cs script, and look for the DistanceToEnemies() coroutine function.


Cette page a-t-elle été utile ?