Hey guys,
im new to all of this so i appreciate any help i can get.
So my problem is the following: I added a sound to my jump but thing is that when i jump, the sound comes delayed by about 0,5 seconds. I dont know why this happens. Silly coding? or just me not knowing something. Btw i use an ogg soundfile
Here is my code.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public float speed = 10f;
public float runSpeed = 15f;
public float jump = 10f;
public float jumpForce = 1f;
private bool isOnGround = false;
private float drScaling;
// Audio
AudioSource audio;
public AudioClip jumpSound;
void Start()
{
drScaling = transform.localScale.x;
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.D))
{
transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
transform.localScale = new Vector2(-drScaling, transform.localScale.y);
}
if (Input.GetKey(KeyCode.A))
{
transform.position -= new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
transform.localScale = new Vector2(+drScaling, transform.localScale.y);
if (Input.GetKey(KeyCode.Space) && isOnGround == true)
{
if(audio.isPlaying == false)
audio.PlayOneShot(jumpSound, 0.3f);
GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Boden")
isOnGround = true;
}
void OnCollisionExit2D(Collision2D coll)
{
isOnGround = false;
}
}