Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Footsteps

Discussion in 'Scripting' started by IchNar1, Oct 30, 2020.

  1. IchNar1

    IchNar1

    Joined:
    Mar 13, 2017
    Posts:
    9
    Hello i have footsteps script but it just doesn't work please help me.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityStandardAssets.Characters.FirstPerson;
    4.  
    5. public class FootstepSounds : MonoBehaviour {
    6.  
    7.     public TextureType[] textureTypes;
    8.  
    9.     public AudioSource audioS;
    10.  
    11.     public RigidbodyFirstPersonController player;
    12.  
    13.     SoundController sc;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         GameObject check = GameObject.FindGameObjectWithTag ("Sound Controller");
    18.  
    19.         if (check != null) {
    20.             sc = check.GetComponent<SoundController> ();
    21.         }
    22.     }
    23.  
    24.     void PlayFootstepSound () {
    25.         RaycastHit hit;
    26.         Vector3 start = transform.position + transform.up;
    27.         Vector3 dir = Vector3.down;
    28.  
    29.         if(player.Grounded && player.Velocity.magnitude > 2f)
    30.         {
    31.             if (Physics.Raycast(start, dir, out hit, 1.3f))
    32.             {
    33.                 if (hit.collider.GetComponent<MeshRenderer>())
    34.                 {
    35.                     PlayMeshSound(hit.collider.GetComponent<MeshRenderer>());
    36.                 }
    37.                 else if (hit.collider.GetComponent<Terrain>())
    38.                 {
    39.                     PlayTerrainSound(hit.collider.GetComponent<Terrain>(), hit.point);
    40.                 }
    41.             }
    42.         }
    43.  
    44.      
    45.     }
    46.  
    47.     void PlayMeshSound (MeshRenderer renderer) {
    48.  
    49.         if (audioS == null) {
    50.             Debug.LogError ("PlayMeshSound -- We have no audio source to play the sound from.");
    51.             return;
    52.         }
    53.  
    54.         if (sc == null) {
    55.             Debug.LogError ("PlayMeshSound -- No sound manager!!!");
    56.             return;
    57.         }
    58.  
    59.         if (textureTypes.Length > 0) {
    60.             foreach (TextureType type in textureTypes) {
    61.  
    62.                 if (type.footstepSounds.Length == 0) {
    63.                     return;
    64.                 }
    65.  
    66.                 foreach (Texture tex in type.textures) {
    67.                     if (renderer.material.mainTexture == tex) {
    68.                         sc.PlaySound (audioS, type.footstepSounds [Random.Range (0, type.footstepSounds.Length)], true, 1, 1.2f);
    69.                     }
    70.                 }
    71.             }
    72.         }
    73.     }
    74.  
    75.     void PlayTerrainSound (Terrain t, Vector3 hitPoint) {
    76.         if (audioS == null) {
    77.             Debug.LogError ("PlayTerrainSound -- We have no audio source to play the sound from.");
    78.             return;
    79.         }
    80.  
    81.         if (sc == null) {
    82.             Debug.LogError ("PlayTerrainSound -- No sound manager!!!");
    83.             return;
    84.         }
    85.  
    86.         if (textureTypes.Length > 0) {
    87.  
    88.             int textureIndex = TerrainSurface.GetMainTexture (hitPoint);
    89.  
    90.             foreach (TextureType type in textureTypes) {
    91.  
    92.                 if (type.footstepSounds.Length == 0) {
    93.                     return;
    94.                 }
    95.  
    96.                 foreach (Texture tex in type.textures) {
    97.                     if (t.terrainData.splatPrototypes[textureIndex].texture == tex) {
    98.                         sc.PlaySound (audioS, type.footstepSounds [Random.Range (0, type.footstepSounds.Length)], true, 1, 1.2f);
    99.                     }
    100.                 }
    101.             }
    102.         }
    103.     }
    104. }
    105.  
    106. [System.Serializable]
    107. public class TextureType {
    108.     public string name;
    109.     public Texture[] textures;
    110.     public AudioClip[] footstepSounds;
    111. }
    112.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SoundController : MonoBehaviour {
    5.  
    6.     public void PlaySound (AudioSource audioS, AudioClip clip, bool randomizePitch = false, float randomPitchMin = 1, float randomPitchMax = 1) {
    7.  
    8.         audioS.clip = clip;
    9.  
    10.         if (randomizePitch == true) {
    11.             audioS.pitch = Random.Range (randomPitchMin, randomPitchMax);
    12.         }
    13.  
    14.         audioS.Play ();
    15.     }
    16.  
    17.     public void InstantiateClip (Vector3 pos, AudioClip clip, float time = 2f, bool randomizePitch = false, float randomPitchMin = 1, float randomPitchMax = 1) {
    18.         GameObject clone = new GameObject ("one shot audio");
    19.         clone.transform.position = pos;
    20.         AudioSource audio = clone.AddComponent<AudioSource> ();
    21.         audio.spatialBlend = 1;
    22.         audio.clip = clip;
    23.         audio.Play ();
    24.  
    25.         Destroy (clone, time);
    26.     }
    27. }
    28.  
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hello, i have his person i'd like to help, but they just didnt tell me what's wrong. ;)
    http://plbm.com/?p=220
     
    Last edited: Oct 30, 2020
  3. IchNar1

    IchNar1

    Joined:
    Mar 13, 2017
    Posts:
    9
    Hmm, it seems like it can't go through this condition.
    Code (CSharp):
    1.  if (hit.collider.GetComponent<MeshRenderer>())
    Can you help me, with this problem? Result of this condition is always null how can i fix it?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    That just means that whatever object you are hitting does not have a MeshRenderer component attached. Or that you do not hit anything at all.

    Since you start the raycast above the player and cast downwards you may be hitting your player. Does your player have a collider but not a mesh renderer component? Either way you do not want to hit the player, so you should probably use a layermask such that the raycast does not hit the player.
    One other thing i note is that you limit your raycast length to 1.3f, which is quite short. Since you only play a sound when the player is grounded, you can make it arbitrarily long, like 10f.
     
  5. IchNar1

    IchNar1

    Joined:
    Mar 13, 2017
    Posts:
    9
    Yes my player has a collider but no mesh renderer, but this object has mesh renderer
    dwasdw.png
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Well yeah, as i said above, if you cast your ray from above the player downwards but do not ignore the collision with the player, then you will always collide with the player, not the ground. I already recommended you what to do above, so i'm not gonna repeat it here :p
     
  7. IchNar1

    IchNar1

    Joined:
    Mar 13, 2017
    Posts:
    9
    Okay, and how can i check if player made only one step?
     
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    "Steps" are a vague. You will have to define that yourself. You could for example remember where the player started walking and check if the position he stops at is between one and two step-sizes (maybe use one 'feet' length) from the place he started moving. Again, how you define all of this is up to you.
    In that case he would have done one single step.
     
  9. IchNar1

    IchNar1

    Joined:
    Mar 13, 2017
    Posts:
    9
    Hm i made something this, but it isn't working perfectly...
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         currentSpeed = GetPlayerSpeed();
    4.         walking = CheckIfWalking();
    5.         if (walking)
    6.         {
    7.             distanceTraveled = (currentSpeed * Time.deltaTime);
    8.             print(distanceTraveled);
    9.             if (distanceTraveled >= 0.035f)
    10.             {
    11.                 PlayFootstepSound();
    12.                 distanceTraveled = 0;
    13.  
    14.             }
    15.         }
    16.        
    17.     }
    18.  
    19. float GetPlayerSpeed()
    20.     {
    21.         float speed = player.Velocity.magnitude;
    22.         return speed;
    23.     }
    24.  
    25.     bool CheckIfWalking()
    26.     {
    27.         if(currentSpeed > 0 && player.Grounded)
    28.         {
    29.             return true;
    30.         }
    31.         else
    32.         {
    33.             return false;
    34.         }
    35.     }
    36.  
    37.  
     
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You check every frame whether some distance was passed, and if so play a sound. So obviously as long as you surpass some speed/frame, you play an audio clip every frame. Instead remember the position you last played your audio at, then play the next when some total distance was passed.
     
  11. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    Your distanceTraveled variable is wrong - your are doing currentSpeed * Time.deltaTime (0.016666, one frame).

    So that could be 3 * 0.016666 which is 0.05, which is greater than 0.035, so it plays the sound and resets distanceTraveled every frame.

    Try using
    distanceTraveled = Vector3.Distance(previousPosition, transform.position);
    then reset it by assigning a new previousPosition.
     
  12. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    here for footsteps, should help you :)

     
    Yoreki likes this.
  13. IchNar1

    IchNar1

    Joined:
    Mar 13, 2017
    Posts:
    9
    Okay and the last question, is it possible to make a layer on this: ?
    dwasdw_LI.jpg