Could someone help me with adding Audio to this code? (Is for a 2D game platformer)
Really stuck trying to get my head around adding audio to this script. I've looked at a fair few different tutorials on YT but they use a different playercontrol script to what I am currently using. Now I am completely lost as to where and what I need to include in this script to allow character enabled sounds (Running and jumping). Please help!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PlayerController : MonoBehaviour { public CharacterController2D controller; public float runSpeed = 40f; float horizontalMove = 0f; bool jump = false; bool crouch = false; public float distance; public LayerMask whatIsLadder; private float inputVertical; public Animator animatorHero; private Rigidbody2D rb; private bool isClimbing; public float climbSpeed = 5f;
private int count;
public Text countText;
public Text winText;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
count = 0;
SetCountText();
winText.text = "";
}
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animatorHero.SetFloat("heroSpeed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
animatorHero.SetBool("heroJump", true);
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
public void OnLanding()
{
animatorHero.SetBool("heroJump", false);
}
public void OnCrouching(bool heroCrouch)
{
animatorHero.SetBool("heroCrouch", heroCrouch);
}
void FixedUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.up, distance, whatIsLadder);
if (hitInfo.collider != null)
{
if (Input.GetKeyDown(KeyCode.W)) ;
isClimbing = true;
}
else
{
isClimbing = false;
}
if (isClimbing == true)
{
inputVertical = Input.GetAxisRaw("Vertical");
rb.velocity = new Vector2(rb.velocity.x, inputVertical * climbSpeed);
rb.gravityScale = 0;
}
else
{
rb.gravityScale = 1;
}
}
void OnTriggerEnter2D(Collider2D coin)
{
if (coin.gameObject.CompareTag("Pickup_Coin"))
{
Destroy(coin.gameObject);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 15)
{
winText.text = "You Win!";
}
}
}
Your answer
Follow this Question
Related Questions
Stop Audio Source Clip while button Press 1 Answer
can i make a loop for each with all audiosources of my project ? 0 Answers
Second AudioClip won't play 0 Answers
Newbie with OnTriggerEnter & Audio Files 0 Answers
How to Play a Sound Twice in a Row 1 Answer