Howdy everyone, I am a very very newbie to coding and was wondering if someone could lend me a hand.
I’ve been using this neat tutorial from Ant Ray Audio to make footsteps sync to FMOD events, but because the game I’m working on is a Top Down game, I’m trying to figure out how to change it so that it relies on a TriggerStay function instead of Raycasts and could use some Like I’m Five Years Old type explanation. Would anybody have any tips or know where I could look to figure it out?
Here’s the code as I’ve copied it from his tutorial:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FMODUnity;
using FMOD.Studio;
public class F_CharFst : MonoBehaviour
{
private int MaterialValue;
private RaycastHit rh;
private float distance = 0.3f;
private string EventPath = "event:/Eoin/Movement/footsteps";
private PARAMETER_ID ParamID;
private PARAMETER_ID ParamID2;
private LayerMask lm;
private EventDescription EventDes;
private PARAMETER_DESCRIPTION ParamDes;
// Start is called before the first frame update
private void Start()
{
// Assigns the ID of 'Terrain'
ParamID.data1 = 1082362436;
ParamID.data2 = 3768982689;
// Assigns the ID of 'WalkRun'
ParamID.data1 = 4001576615;
ParamID.data2 = 3268475985;
lm = LayerMask.GetMask("Ground");
}
// Update is called once per frame
void Update()
{
//Debug.DrawRay(transform.position, Vector3.down * distance, Color.blue);
}
void PlayWalkEvent()
{
//Material Check and Sound Instantiation
MaterialCheck();
EventInstance Walk = RuntimeManager.CreateInstance(EventPath);
RuntimeManager.AttachInstanceToGameObject(Walk, transform, GetComponent<Rigidbody>());
//Sets Terrain Parameter
Walk.setParameterByID(ParamID, MaterialValue, false);
Walk.start();
Walk.release();
}
void MaterialCheck()
{
if (Physics.Raycast(transform.position, Vector3.down, out rh, distance, lm))
{
switch (rh.collider.tag)
{
case "dirt":
MaterialValue = 0;
break;
case "leaves":
MaterialValue = 1;
break;
case "stone":
MaterialValue = 2;
break;
case "grass":
MaterialValue = 3;
break;
case "wood":
MaterialValue = 4;
break;
case "snow":
MaterialValue = 5;
break;
}
}
}
}
Hi,
This might be a question more appropriate for the Unity forums.
If you are looking at using the OnTriggerStay() function then you could just change the MaterialCheck()
function like so:
private void OnTriggerStay(Collider other)
{
if (other != null)
{
MaterialCheck(other);
}
}
void MaterialCheck(Collider other)
{
switch (other.tag)
{
case "dirt":
MaterialValue = 0;
break;
case "leaves":
MaterialValue = 1;
break;
case "stone":
MaterialValue = 2;
break;
case "grass":
MaterialValue = 3;
break;
case "wood":
MaterialValue = 4;
break;
case "snow":
MaterialValue = 5;
break;
default:
MaterialValue = 0;
break;
}
}
What I have done is change the MaterialCheck()
to use the collider passed in from the OnTriggerStay()
.
Hope this helps!
1 Like
Yeah I realize now this is more a C++ question than a FMOD one, I can take it there too if that’d be more appropriate. I appreciate the help anyway!
Ah, I was figuring I had to do something like that I just wasn’t sure how to set the TriggerStay up to reference MaterialCheck. I’m now running into the error in line 40 with the PlayWalkEvent function where it’s saying “There is no argument given that corresponds to the required formal parameter ‘other’ of ‘F_CharFst.MaterialCheck(Collider)’”
From what I’m finding this means I have to call Parameters along with MaterialCheck() when it shows up in PlayWalkEvent because of how it’s set up but I’m not sure what those would be.
I can ask in the Unity forums if it’s not something you feel like getting into.
1 Like
Hi,
The issue is we are still calling MaterialCheck()
in PlayWalkEvent()
. Remove the MaterialCheck()
function then the error should stop.
Hope this helps!
Ah, yep, that fixed that. Thank you for your infinite patience.
So I now have a more peculiar and FMOD specific issue. I know that the event is being played correctly on some level because the default sound correctly plays and I can still modulate the sound in other ways (volume, panning, etc…) using live update. Also, if I change the default value of the parameter in the script, it correctly plays that new default (ie, if dirt is default, the dirt sound will play in Unity’s play mode, if snow is default, the snow sound will play in Unity’s default).
However, not only does changing the collider being interacted with not seem to affect the FMOD parameter, but changing the parameter manually in FMOD using live update doesn’t change the sound played in Unity (ie, if dirt is default, the dirt sound will play, and if I manually switch the parameter to snow in FMOD, it still plays the dirt sound, but changing volume etc… still works). I was initially thinking it was just how my Triggers and Colliders are set up but other things seem to be working related to those.
Edit: should add for clarification, switching the parameter in FMOD and playing the event in FMOD’s own mixer does also work as intended, it’s that switching the parameter in FMOD seems to have no effect in Unity unless the default is changed in the script
1 Like
Hi,
Live Update cannot affect parameters attached to instances in Unity. You are only able to view them.
Could you elaborate on the issue here? If you add some debug logs to the MaterialCheck()
are you getting the expected behavior?
1 Like
Good to know about LiveUpdate! I’d messed around with some volume parameters when testing some 2D distance stuff and they’d changed things before but that might just be because I have buses set up? Or just because it’s directly tied to volume?
I ended up figuring out a solution. I had more than one box collider on my character object to interact with the ground and they were giving each other issues, and then (foolishly) I’d forgotten I’d put my animator and rigidbody on separate components. So I created a “Terrain” layer exclusively for the sounds and swapped out the GetComponent command for GetComponentInParent and now everything works.
Thanks so much for all the help!
1 Like
No worries! Thank you for sharing the solution!
If you have any more questions please do not hesitate to ask!