A tiny sound bug I'm not sure how to solve.
The character jumps, he hits the ground and a sound is played. It works but if you are about to fall and for that brief moment when the character is hitting the ground and the sound is played if you jump right at that moment the variables which enables the sound to play will be true a second time which makes the sound play twice. I tried disabling the jump for 0.1 seconds after you land but still if you hop around long enough the bug comes back. Any ideas on how to fix this? (Ignore the stomp)
//Player jumping
if (JumpingEnabled)
{
if (Input.GetButtonDown("Jump") && touchesGround)
{
Jump.Play();
playerRigidBody.AddForce(0f, 325f, 0f);
secondJump = true;
}
else if (Input.GetButtonDown("Jump") && secondJump == true)
{
StartCoroutine("Stomp");
}
else if (secondJump == true && touchesGround)
{
secondJump = false;
ComesToGround.Play();
StartCoroutine("DisableJump");
}
else
{
}
}
IEnumerator DisableJump()
{
JumpingEnabled = false;
yield return new WaitForSeconds(0.1f);
JumpingEnabled = true;
}
Comment
Best Answer
Answer by Haugkall · Mar 11, 2018 at 03:25 PM
I figured it out, had to not associate the sound with the jump.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Character : MonoBehaviour
{
//Todo
//•How to multiply rotation by deltaTime?
//•Why can't I give the jump a float?
Transform playerTransform;
Rigidbody playerRigidBody;
public AudioSource Jump;
public AudioSource Stomping;
public AudioSource AboutToStomp;
public AudioSource ComesToGround;
public AudioSource Tail1;
public AudioSource Tail2;
public AudioSource Tail3;
public GameObject Stomper;
public GameObject TailAttack;
GameObject Ground;
float characterFSpeed = 4.5f;
float characterBSpeed = 1.75f;
public bool touchesGround;
public bool secondJump = true;
public bool playerMovementEnabled = true;
public bool stompingSoundPlayer = false;
public bool JumpingEnabled = true;
public bool tailAttacking = true;
public bool fallsoundactivated;
void Start()
{
playerTransform = GetComponent<Transform>();
playerRigidBody = GetComponent<Rigidbody>();
playerRigidBody.freezeRotation = true;
}
void Update()
{
if (tailAttacking == true)
{
if (Input.GetButtonDown("Fire3"))
{
StartCoroutine("TailAttacking");
}
}
//Is player movement enabled?
if (playerMovementEnabled == true)
{
//Player rotation.
playerTransform.Rotate(0, Input.GetAxis("Horizontal"), 0);
//Player movement.
float vertical = Input.GetAxis("Vertical");
if (vertical > 0)
{
vertical = characterFSpeed;
}
if (vertical < 0)
{
vertical = -characterBSpeed;
}
playerTransform.Translate(0, 0, vertical * Time.deltaTime);
}
//Player jumping
if (JumpingEnabled)
{
if (Input.GetButtonDown("Jump") && touchesGround)
{
Jump.Play();
playerRigidBody.AddForce(0f, 325f, 0f);
secondJump = true;
}
else if (Input.GetButtonDown("Jump") && secondJump == true)
{
StartCoroutine("Stomp");
}
else if (secondJump == true && touchesGround)
{
JumpingEnabled = false;
secondJump = false;
StartCoroutine("DisableJump");
}
else
{
}
}
if (touchesGround == false)
{
fallsoundactivated = true;
}
if (touchesGround && fallsoundactivated == true)
{
Debug.Log("Detected");
ComesToGround.Play();
fallsoundactivated = false;
}
//When the character hits the ground after having stomped a sound is played.
if (stompingSoundPlayer == true && touchesGround)
{
JumpingEnabled = false;
Stomping.Play();
stompingSoundPlayer = false;
StartCoroutine("Stompdelay");
}
}
IEnumerator TailAttacking()
{
tailAttacking = false;
TailAttack.SetActive(true);
//Add two more tail sounds here later when you know how.
Tail1.Play();
yield return new WaitForSeconds(0.3f);
TailAttack.SetActive(false);
yield return new WaitForSeconds(0.5f);
tailAttacking = true;
}
//Disables for 0.1 seconds.
IEnumerator DisableJump()
{
yield return new WaitForSeconds(0.1f);
JumpingEnabled = true;
yield return new WaitForSeconds(1f);
}
//A delay before the character is able to jump again.
IEnumerator Stompdelay()
{
yield return new WaitForSeconds(0.5f);
Stomper.SetActive(false);
playerMovementEnabled = true;
JumpingEnabled = true;
}
//The character freezes in the air and stomps towards the ground.
IEnumerator Stomp()
{
JumpingEnabled = false;
Stomper.SetActive(true);
playerMovementEnabled = false;
playerRigidBody.isKinematic = true;
AboutToStomp.Play();
yield return new WaitForSeconds(0.75f);
playerRigidBody.isKinematic = false;
stompingSoundPlayer = true;
secondJump = false;
playerRigidBody.AddForce(0f, -1500f, 0f);
}
void OnCollisionEnter(Collision Ground)
{
if (Ground.gameObject.tag == "Ground")
{
touchesGround = true;
fallsoundactivated = true;
}
}
void OnCollisionExit(Collision Ground)
{
if (Ground.gameObject.tag == "Ground")
{
touchesGround = false;
}
}
}