This question was
closed Aug 25, 2019 at 02:54 PM by
lCedric.
2D jump sound not working.
Hello! I am having trouble getting my jump sound to play whenever i am jumping! I would appriciate any help i can get. I am pretty new to coding so please explain it in a way that i can easily understand it. I have no clue what to do or how to fix it, because in my eyes it looks perfectly fine.
My script (c#):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
Rigidbody2D rb;
private Animator anim;
public float speed;
private float moveInput;
private bool isGrounded;
public Transform feetpos;
public float checkradius;
public LayerMask WhatisGround;
public float Jumpforce;
AudioSource audio;
public AudioClip ActualJump2D;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, -180, 0);
}
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
isGrounded = Physics2D.OverlapCircle(feetpos.position, checkradius, WhatisGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("isJumping", true);
rb.velocity = Vector2.up * Jumpforce;
audio.PlayOneShot(ActualJump2D, 1F);
}
if (rb.velocity.y == 0)
{
anim.SetBool("isJumping", false);
}
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name.Equals("MovingSlab11"))
this.transform.parent = col.transform;
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.name.Equals("MovingSlab11"))
this.transform.parent = null;
}
}
Thanks for the help! <3
Comment
Answer by busybyte · Jul 09, 2019 at 11:43 AM
I got some similar problem because i started play, for many times, within the update function. So there was no time to play it to the end. Maybe a check, if audio is already playing, helps you too, otherwise check your player-settings like playOnAwake,spatialBlend...
if (!audio.isPlaying) audio.PlayOneShot(ActualJump2D, 1F);