Search Unity

Audio Audio Help

Discussion in 'Audio & Video' started by BookwormGames, Jan 15, 2019.

  1. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    Hello! I'm making a 2D platformer demo, and my last big goal is to get sound into my game. I have 4 audio files for my character (Used for Running, Jumping, Landing, and Death), and 2 for coins. Would someone be able to show me how to program sound playing into my codes? Below I will put in the scripts for my Player and the Coins. I apologize if this sounds picky or whiny or anything like that, but would you be able to keep the instructions simple if possible? I have no experience with coding/programming before this project, and these codes were seen in tutorials. Thank you for your time and help!
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     public float speed = 5f;
    4.     public float jumpSpeed = 8f;
    5.     private float movement = 0f;
    6.     private Rigidbody2D rigidBody;
    7.     public Transform groundCheckPoint;
    8.     public float groundCheckRadius;
    9.     public LayerMask groundLayer;
    10.     private bool isTouchingGround;
    11.     private Animator playerAnimation;
    12.     public Vector3 respawnPoint;
    13.     public LevelManager gameLevelManager;
    14.  
    15.     Vector2 tmpTransform;
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         rigidBody = GetComponent<Rigidbody2D>();
    21.         playerAnimation = GetComponent<Animator>();
    22.         respawnPoint = transform.position;
    23.         gameLevelManager = FindObjectOfType<LevelManager>();
    24.         tmpTransform = transform.localScale;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
    31.         movement = Input.GetAxis("Horizontal");
    32.         if (movement > 0f)
    33.         {
    34.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    35.             transform.localScale = tmpTransform;
    36.                
    37.         }
    38.         else if (movement < 0f)
    39.         {
    40.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    41.             transform.localScale = new Vector2(-tmpTransform.x, tmpTransform.y);
    42.         }
    43.         else
    44.         {
    45.             rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
    46.         }
    47.         if (Input.GetButtonDown("Jump") && isTouchingGround)
    48.         {
    49.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
    50.         }
    51.         playerAnimation.SetFloat("Speed", Mathf.Abs(rigidBody.velocity.x));
    52.         playerAnimation.SetBool("OnGround", isTouchingGround);
    53.     }
    54.  
    55.     private void OnTriggerEnter2D(Collider2D other)
    56.     {
    57.         if (other.tag == "FallDetector")
    58.         {
    59.             gameLevelManager.Respawn();
    60.         }
    61.         if (other.tag == "CheckPoint")
    62.         {
    63.             respawnPoint = other.transform.position;
    64.         }
    65.     }
    66.  
    67.  
    68. }
    Code (CSharp):
    1. public class CoinScript : MonoBehaviour {
    2.     private LevelManager gameLevelManager;
    3.     public int coinValue;
    4.     private AudioSource coinSoundEffect;
    5.     public AudioClip coin;
    6.     public float volLowRange = 0.5f;
    7.     public float volHighRange = 1.0f;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         gameLevelManager = FindObjectOfType<LevelManager> ();
    12.         coinSoundEffect = GetComponent<AudioSource>();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.     }
    19.  
    20.     void OnTriggerEnter2D(Collider2D other) {
    21.         if(other.tag == "Player") {
    22.             coinSoundEffect.PlayOneShot(coin, 1F);
    23.             gameLevelManager.AddCoins(coinValue);
    24.             Destroy(gameObject);
    25.         }
    26.     }
    27. }