Here is my code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rocket : MonoBehaviour
{
Rigidbody rigidBody;
AudioSource AudioSrc;
bool AudioPlaying;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
AudioSrc = GetComponent<AudioSource>();
AudioPlaying = false;
}
// Update is called once per frame
void Update()
{
ProcessInput();
}
private void ProcessInput()
{
Thrust();
Rotate();
}
private void Thrust()
{
if (Input.GetKey(KeyCode.Space)) //can thrust while rotating
{
rigidBody.AddRelativeForce(Vector3.up);
if (!AudioPlaying)
{
AudioSrc.Play();
AudioPlaying = true;
}
}
else
{
AudioSrc.Stop();
AudioPlaying = false;
}
}
private void Rotate()
{
if (Input.GetKey(KeyCode.A) & !Input.GetKey(KeyCode.D)) //rotate left, can't rotate both directions at the same time
{
rigidBody.freezeRotation = true;// take manual control of rotation
transform.Rotate(Vector3.forward);
rigidBody.freezeRotation = false; // resume physics control of rotation
}
if (Input.GetKey(KeyCode.D) & !Input.GetKey(KeyCode.A)) //rotate right, can't rotate both directions at the same time
{
rigidBody.freezeRotation = true; // take manual control of rotation
transform.Rotate(-Vector3.forward);
rigidBody.freezeRotation = false; // resume physics control of rotation
}
}
}
Let me know if you have any comments/recommendations.
!