Search Unity

Question Footsteps Audio - Material Change Overlapping Colliders - script help needed

Discussion in 'Scripting' started by Alexapo, Apr 13, 2021.

  1. Alexapo

    Alexapo

    Joined:
    Apr 6, 2021
    Posts:
    10
    So I am trying to create Footsteps sounds in Unity with Wwise and change the sounds depending on what type of material the player is walking on.

    What I have done is create box colliders for the spesific material I want to make a different sound for and a default sound that plays when the player is not in this area. So, there is a default sound that plays a footsteps sounds, and when the player enters the box collider, the footstep sound changes, and when the player exits the colliders, the sound changes to the default sound. This is how the colliders look:

    Utklipp1.png

    The way I have it set up works - ish. On each of the colliders I have this script:

    Utklipp2.png
    I also have the colliders set to "Is Trigger".
    Uklipp 3.png

    What happens is that when the player exits one collider and enters an overlapping collider, the OnTriggerExit still triggers instead of the new OnTriggerEnter of the overlapping collider. I am not a programmer, so this might be an easy fix, but if anyone could help with this that would be great. The goal is to make sure that the OnTriggerExit only triggers as long as the player exits the collider completely and not enters an overlapping collider.

    If there for example is a way to check if the player is still inside a collider when the OnTriggerExit function is triggered that says that if the player is still inside a collider, do not change the sound or something like that.

    Any help would be appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    OnTriggerStay() will fire as long as a player is in a trigger.

    However, it might be best to just only use OnTriggerEnter() and set the new material each time you enter it.

    This basically assumes you cannot be on two materials at the same time: whichever one you USED to be on will be replaced by the new one on Enter().