Hi, so I think I must’ve missed something a while back in regard to how I have my Time.deltatime setup for the ApplyThrust methods that is used to propel the rocket. I was hoping to get a full snapshot of the finalized code that propels the rocket up, because right now I have my thrust variable set to 100,000 to make it work well, so something is definitely up. It took me a while to notice my mistake, since the program still works, but specifically my “ApplyThrust” and “RespondToThrustInput” seem to be slightly off, and I haven’t figured out what simple thing I missed.
Really hoping to get help with this before moving onto the next project, as I’m nearing the end of this project and want to have a solid understanding of this one’s function.
Any and all help is appreciated! Code is below, and I specifically think I messed up my methods mentioned above.
public class Rocket : MonoBehaviour
{
Rigidbody rigidBody;
AudioSource audioSource;
[SerializeField] float rcsThrust = 200f;
[SerializeField] float mainThrust = 3000f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip successSound;
[SerializeField] AudioClip deathSound;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem successSoundParticles;
[SerializeField] ParticleSystem deathSoundParticles;
enum State {alive, dying, transcending}
State state = State.alive;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (state == State.alive)
{
RespondToThrustInput();
RespondToRotationInput();
}
}
void RespondToThrustInput()
{
float thrustThisFrame = mainThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.Space)) //are you hitting space bar? Regardless, are you hitting D or A?
{
ApplyThrust(thrustThisFrame);
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
private void ApplyThrust(float thrustThisFrame)
{
rigidBody.AddRelativeForce(Vector3.up * thrustThisFrame * Time.deltaTime);
if (!audioSource.isPlaying) //avoids layering
{
audioSource.PlayOneShot(mainEngine);
}
if (!mainEngineParticles.isPlaying)
{
mainEngineParticles.Play();
}
}
void RespondToRotationInput()
{
rigidBody.freezeRotation = true; // take manual control of rotation
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false; // resume physics control
}
void OnCollisionEnter(Collision collision)
{
if (state != State.alive) {return;}
switch (collision.gameObject.tag)
{
case "Friendly":
//do nothing
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.transcending;
audioSource.Stop();
audioSource.PlayOneShot(successSound);
successSoundParticles.Play();
Invoke("LoadNextScene", 1f);
}
private void StartDeathSequence()
{
print("hit something deadly");
state = State.dying;
audioSource.Stop();
audioSource.PlayOneShot(deathSound);
deathSoundParticles.Play();
Invoke("RestartGame", 1f);
}
void RestartGame()
{
SceneManager.LoadScene(0);
}
void LoadNextScene()
{
SceneManager.LoadScene(1); // todo allow for more than 2 levels
}
}```