I’ve been loving this Unity black friday deal course. I’m building the cartoony rocket game right now and adding SFX (re: this lesson). This audio plays without any keyboard input after I start the scene and I think it’s trying to play over itself every frame, sounds like a stuttering buzzing. I’m trying to move on but it bugs me, because I thought I had it right. Here’s my AudioSource inspector:
and my script. I really appreciate any eyeballs you can share. Thanks!
using UnityEngine;
using UnityEngine.InputSystem;
public class Movement : MonoBehaviour
{
[SerializeField] InputAction thrust;
[SerializeField] InputAction rotation;
[SerializeField] float thrustStrength = 100f;
[SerializeField] float rotationStrength = 50f;
Rigidbody rb;
AudioSource audioSource;
private void OnEnable()
{
thrust.Enable();
rotation.Enable();
}
private void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
private void FixedUpdate()
{
ProcessThrust();
ProcessRotation();
}
private void ProcessThrust()
{
if (thrust.IsPressed())
rb.AddRelativeForce(Vector3.up * thrustStrength * Time.fixedDeltaTime);
if (!audioSource.isPlaying)
{
audioSource.Play();
}
else
{
audioSource.Stop();
}
}
private void ProcessRotation()
{
float rotationInput = rotation.ReadValue<float>();
//Debug.Log("Rotation Value is: "+ rotationInput);
if (rotationInput < 0)
{
ApplyRotation(rotationStrength);
}
else if (rotationInput > 0)
{
ApplyRotation(-rotationStrength);
}
}
private void ApplyRotation(float rotationThisFrame)
{
rb.freezeRotation = true;
transform.Rotate(Vector3.forward * rotationThisFrame * Time.fixedDeltaTime);
rb.freezeRotation = false;
}
private void Update()
{
}
}