Audio not playing, but plays on enabling the loop option

Here is my code

using UnityEngine;
using UnityEngine.SceneManagement;

public class movement : MonoBehaviour
{
    Rigidbody rb;
    AudioSource MaudioSoure;
    public float thrust;
    public float Rotation;


    void Start()
    {
        rb = GetComponent<Rigidbody>();
        MaudioSoure = GetComponent<AudioSource>();
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            rb.AddRelativeForce(Vector3.up * thrust * Time.deltaTime);
            if(!MaudioSoure.isPlaying)
            { 
                MaudioSoure.Play();
            }
        }
        else
        { 
            MaudioSoure.Stop();
        }

        if (Input.GetKey(KeyCode.D))
        {
            rb.freezeRotation = true;
            ApplyRotation(-Rotation);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            rb.freezeRotation = true;
            ApplyRotation(Rotation);
        }
        else
        {
            rb.freezeRotation = false;
            rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationY;
        }
    }

    void ApplyRotation(float direction)
    {
        transform.Rotate(0, 0, direction * Time.deltaTime);
    }
}

Think it was a bug, I just detached and attached the audio source from player and it worked…

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms