Last active
October 4, 2023 18:10
-
-
Save athiedmann/ef53ce6c472ad847e692345ae7248224 to your computer and use it in GitHub Desktop.
Footstep Manager script for Unity + Wwise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class ThirdPersonPlayerFootstepManager : MonoBehaviour | |
| { | |
| // Enum to represent different material type | |
| private enum CURRENT_MATERIAL { STONE, PUDDLE, GRASS, EARTH, VEGETATION }; | |
| [SerializeField] bool materialCheckConfigured = false; | |
| [Header("Wwise Events")] | |
| [SerializeField] private AK.Wwise.Event playerFootstep; | |
| [SerializeField] private AK.Wwise.Event playerLanding; | |
| [Header("Material Check")] | |
| [SerializeField] private CURRENT_MATERIAL currentMaterial; // Displays current material detected in Editor | |
| [SerializeField] AK.Wwise.Switch[] groundMaterialSwitch; // Array of Wwise switches for ground material | |
| private void FixedUpdate() | |
| { | |
| if (materialCheckConfigured) | |
| { | |
| MaterialCheck(); | |
| } | |
| else | |
| { | |
| return; | |
| } | |
| } | |
| #region Player's Methods | |
| /// <summary> | |
| /// Called by animation events | |
| /// </summary> | |
| void PlayFootstepsEvent() | |
| { | |
| if (playerFootstep.IsValid()) | |
| playerFootstep.Post(gameObject); | |
| } | |
| /// <summary> | |
| /// Called by animation events | |
| /// </summary> | |
| /// <param name="landingType">Has a value of 0 for "Idle", 1 for "Run", 2 for "Sprint", 3 for "Walk"</param> | |
| void PlayLandingEvent(int landingType) | |
| { | |
| if (playerLanding.IsValid()) | |
| { | |
| switch (landingType) | |
| { | |
| case 0: | |
| AkSoundEngine.SetSwitch("PlayerLandingSwitch", "Idle", gameObject); | |
| playerLanding.Post(gameObject); | |
| break; | |
| case 1: | |
| AkSoundEngine.SetSwitch("PlayerLandingSwitch", "Run", gameObject); | |
| playerLanding.Post(gameObject); | |
| break; | |
| case 2: | |
| AkSoundEngine.SetSwitch("PlayerLandingSwitch", "Run_Fast", gameObject); | |
| playerLanding.Post(gameObject); | |
| break; | |
| case 3: | |
| AkSoundEngine.SetSwitch("PlayerLandingSwitch", "Walk", gameObject); | |
| playerLanding.Post(gameObject); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| else | |
| { | |
| Debug.LogWarning($"No event assigned to ellenLanding at {gameObject.name}"); | |
| return; | |
| } | |
| } | |
| // Perform material check based on raycast hits | |
| private void MaterialCheck() | |
| { | |
| RaycastHit[] hits = Physics.RaycastAll(transform.position, Vector3.down, 0.1f); | |
| int environmentLayer = LayerMask.NameToLayer("Environment"); | |
| int waterGeometryLayer = LayerMask.NameToLayer("WaterGeometry"); | |
| int vegetationLayer = LayerMask.NameToLayer("Vegetation"); | |
| int setDressingLayer = LayerMask.NameToLayer("SetDressing"); | |
| AK.Wwise.Switch switchToSet = null; // Variable to store the switch to set | |
| foreach (RaycastHit hit in hits) | |
| { | |
| GameObject hitObject = hit.transform.gameObject; | |
| int hitLayer = hitObject.layer; | |
| string hitTag = hit.collider.tag; | |
| if (hitLayer == environmentLayer) | |
| { | |
| if (hitTag == "Material: Stone") | |
| { | |
| currentMaterial = CURRENT_MATERIAL.STONE; | |
| switchToSet = groundMaterialSwitch[3]; | |
| } | |
| else | |
| { | |
| currentMaterial = CURRENT_MATERIAL.EARTH; | |
| switchToSet = groundMaterialSwitch[0]; | |
| } | |
| } | |
| else if (hitLayer == waterGeometryLayer) | |
| { | |
| currentMaterial = CURRENT_MATERIAL.GRASS; | |
| switchToSet = groundMaterialSwitch[1]; | |
| } | |
| else if (hitLayer == vegetationLayer) | |
| { | |
| currentMaterial = CURRENT_MATERIAL.VEGETATION; | |
| switchToSet = groundMaterialSwitch[1]; | |
| } | |
| else if (hitLayer == setDressingLayer) | |
| { | |
| currentMaterial = CURRENT_MATERIAL.VEGETATION; | |
| switchToSet = groundMaterialSwitch[1]; | |
| } | |
| else | |
| { | |
| currentMaterial = CURRENT_MATERIAL.STONE; | |
| switchToSet = groundMaterialSwitch[3]; | |
| } | |
| } | |
| // Set the switch outside the loop | |
| if (switchToSet != null) | |
| { | |
| switchToSet.SetValue(gameObject); | |
| } | |
| } | |
| #endregion | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment