when ever i add a sound for jump Button the player start to jump immediately even if you didnt click
everything work fine without adding the sound
once add the player will jump 100 time in single secend
the button
here is the entire code:
public class MovesByButtons : MonoBehaviour
{
public float JumpSpeed;
public float speed;
private float horizontalMove;
private bool moveRight;
private bool moveLeft;
private Rigidbody2D rb;
[SerializeField] private AudioSource JumpSoundEffect;
void Start()
{
rb = GetComponent<Rigidbody2D>();
moveLeft = false;
moveRight = false;
}
// Update is called once per frame
void Update()
{
Movement();
}
public void pointerDownLeft()
{
moveLeft = true;
}
public void pointerUpLeft()
{
moveLeft = false;
}
public void pointerDownRight()
{
moveRight = true;
}
public void pointerUpRight()
{
moveRight = false;
}
void Movement()
{
if (moveLeft)
{
horizontalMove = -speed;
}
else if (moveRight)
{
horizontalMove = speed;
}
else
{
horizontalMove = 0;
}
}
public void jumpbutton()
{
rb.velocity = Vector2.up * JumpSpeed;
}
private void FixedUpdate()
{
rb.velocity = new Vector2(horizontalMove,rb.velocity.y);
JumpSoundEffect.Play();
rb.velocity = new Vector2(0f, JumpSpeed);
}
}
just like i said everything work fine till i put the sound then the player jump infint times
the code for sound is
rb.velocity = new Vector2(horizontalMove,rb.velocity.y);
JumpSoundEffect.Play();