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

[C#] Footstep audio script

Discussion in 'Scripting' started by Sacroslash, May 18, 2013.

  1. Sacroslash

    Sacroslash

    Joined:
    May 1, 2013
    Posts:
    5
    Hi all,

    I've been wrestling with a footstep audio script for days now, and I can't get it to work. The script I have right now is the closest I got to a working one, but the '' command doesn't work with bool and float. Any suggestions?

    The basic thing I'm trying to do here is that I have 2 sorts of ground the character is going to walk on: water, and grass. I have tagged both of these surfaces accordingly. It's just walking, I'm not going to incorporate running of any sort into my game. Hope you guys can help!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Playerstats : MonoBehaviour {
    5.     float AudioTimer = 0;
    6.     AudioClip GrassSound;
    7.     AudioClip WaterSound;
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if(AudioTimer > 0){
    17.         AudioTimer -= Time.deltaTime;  
    18.         }
    19.         if(AudioTimer < 0){
    20.         AudioTimer = 0;
    21.         }
    22.     }
    23.    
    24.     void OnControllerColliderHit (ControllerColliderHit col) {
    25.       if (col.gameObject.CompareTag("Water")  Input.GetAxis("Vertical")  AudioTimer == 0 ||
    26.         Input.GetAxis("Horizontal")  AudioTimer == 0) {
    27.         audio.clip = WaterSound;
    28.         audio.PlayOneShot (WaterSound);
    29.         AudioTimer = 0.3f;
    30.         }
    31.     if(col.gameObject.CompareTag("Grass")  Input.GetAxis("Vertical")  AudioTimer == 0
    32.  || Input.GetAxis("Horizontal")  AudioTimer == 0) {
    33.         audio.clip = GrassSound;
    34.         audio.PlayOneShot(GrassSound);
    35.         AudioTimer = 0.3f;
    36.         }
    37.     }
    38. }
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Seems like a lot of work. Checking stuff etc. I think it could be simpler.

    Why do you need to check you input.axis?


    Also, you audioTimer is unnecessary. Just use isPlaying.

    So I would do something like...
    Code (csharp):
    1.  
    2. ///onControllerCollderHit
    3. if(col.tag == "Water"  !audio.isPlaying){
    4. audio.clip = WaterSound;
    5. audio.Play();
    6. return;
    7. }
    8.  

    No need for update and the massive check list.
     
    Batz_ likes this.
  3. Sacroslash

    Sacroslash

    Joined:
    May 1, 2013
    Posts:
    5
    Thanks for the help!

    Thought the InputAxis was necessary to check if your character is actually moving to play the sound. And you're right, the Audiotimer is pretty unnecessary, but was suggested to me by someone because it would come in 'handy'. Not so much.

    However, the 'col.tag' gives me a CS1061 error, probably need some sort of assembly reference? Tried a couple of different things as wel, but it isn't working.
    Also, I only need the sound to play if the player is actually moving - doesn't just standing still also count as a collission?
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Right, perhaps to check the movement is correct. Sorry.

    As far the timer and using isPlaying, is that, you don't need to check for it. You don't need to set the value. It is much cleaner to use isPlaying.

    col.tag, perhaps is incorrect. I do not use onControllerColliderHit. If compareTag is working just go with that. I thought it was just saving you a step. col.Tsg, is less work than col.gameObject.CompareTag()
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Also, I just looked it up. With audio.PlayOneShot(), you do not need to assign the audio.clip via audio.clip = this. So, that might be better too. Boy! I am learning all types of stuff today.


    Also, the second check, for is grass. You have seemed to have left out the || declaration you have for water.
     
    Last edited: May 18, 2013
  6. Sacroslash

    Sacroslash

    Joined:
    May 1, 2013
    Posts:
    5
    Haha, both you and me. The sound is playing right now, but like I thought, plays as soon as my character touches the ground. the Input GetAxis won't work, because of the same reason from my first post, because of bool/float. Somebody knows how to get this to work properly?

    Also, it doesn't seem to play the Water sound as soon as I walk on my water surface, even though it is tagged properly.

    This is what the code looks like right now. You can ignore the health, ghostsfound and void OnCollisionEnter, seeing as that is for something else. However, I thought I might include it to see if the OnCollisionEnter doesn't somehow clash with the OnControllerColliderHit.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Playerstats : MonoBehaviour {
    5.     public int health = 1; 
    6.     public int ghostsfound = 0;
    7.     ControllerColliderHit col;
    8.     public AudioClip GrassSound;
    9.     public AudioClip WaterSound;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.     }
    20.    
    21.     void OnCollisionEnter (Collision myCollision) {
    22.             if (myCollision.gameObject.tag == "Enemy")
    23.             {
    24.             health--;
    25.             }
    26.             if(health < 1)
    27.             {
    28.                 Application.LoadLevel("GameOver2");
    29.             }
    30.     }
    31.    
    32.     void OnControllerColliderHit (ControllerColliderHit col) {
    33.     if(col.gameObject.CompareTag("Water")  !audio.isPlaying){
    34.     audio.PlayOneShot(WaterSound);
    35.     return;
    36.     }
    37.     else if(col.gameObject.CompareTag("Grass")  !audio.isPlaying){
    38.     audio.PlayOneShot (GrassSound);
    39.     return;
    40.         }
    41.    
    42. }
     
  7. NitroZoron

    NitroZoron

    Joined:
    Jun 29, 2020
    Posts:
    42
    I could use some help too

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.  
    9.     public float speed = 4.7f;
    10.     public float gravity = -9.81f;
    11.     public float jumpHeigth = 3f;
    12.  
    13.     public Transform groundCheck;
    14.     public float groundDistance = 0.4f;
    15.     public LayerMask groundMask;
    16.  
    17.     Vector3 velocity;
    18.     bool isGrounded;
    19.  
    20.     public AudioClip Footstep;
    21.     CharacterController cc;
    22.     public bool isMoving = false;
    23.  
    24.     // Update is called once per frame
    25.     void Start()
    26.         {
    27.         cc = GetComponent<CharacterController>();
    28.         isMoving = false;
    29.         }
    30.  
    31.     void Update()
    32.     {
    33.         if(isMoving)
    34.         {
    35.             GetComponent<AudioSource>().PlayOneShot(Footstep);
    36.         }
    37.      
    38.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    39.  
    40.         if (isGrounded && velocity.y < 0)
    41.         {
    42.             velocity.y = -2f;
    43.         }
    44.  
    45.         float x = Input.GetAxis("Horizontal");
    46.         float z = Input.GetAxis("Vertical");
    47.  
    48.         Vector3 move = transform.right * x + transform.forward * z;
    49.  
    50.         controller.Move(move * speed * Time.deltaTime);
    51.  
    52.         if (Input.GetButtonDown("Jump") && isGrounded)
    53.         {
    54.             velocity.y = Mathf.Sqrt(jumpHeigth * -2f * gravity);
    55.         }
    56.  
    57.         velocity.y += gravity * Time.deltaTime;
    58.  
    59.         controller.Move(velocity * Time.deltaTime);
    60.  
    61.  
    62.         if (cc.isGrounded == true && Input.GetKey("w"))
    63.         {
    64.             isMoving = true;
    65.         }
    66.         if (cc.isGrounded == true && Input.GetKey("a"))
    67.         {
    68.             isMoving = true;
    69.         }
    70.         if (cc.isGrounded == true && Input.GetKey("s"))
    71.         {
    72.             isMoving = true;
    73.         }
    74.         if (cc.isGrounded == true && Input.GetKey("d"))
    75.         {
    76.             isMoving = true;
    77.         }
    78.  
    79.  
    80.  
    81.         if (Input.GetKey(KeyCode.LeftShift))
    82.         {
    83.             speed = 12f;
    84.         }
    85.         else
    86.         {
    87.             speed = 4.7f;
    88.         }
    89.        
    90.     }
    91. }