Thrust not working - no vertical movement

My vertical thrust has stopped working. (I had it working fine until the refactoring).
I can rotate the rocket but I can’t make it move (via space bar).
I downloaded the course materials scripts and applied them to my model and yet it still does not move vertiically. (no compiler errors).
The particle system is working for up/left/right and the sounds are partially. When the mainEngine sound is triggered for the thrusters I get a buzz and not the same sound as the main thruster sound - even though the code says to play the same sound.

Any suggestions?

Hi,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? We move the rocket by passing on a value to the AddRelativeForce method. If the value on the y-axis is 0 or too close to 0, the rocket will not move vertically. The y-velocity must be higher than the y-gravity.

Regarding the sound issue, that sounds like a problem in the code. A common mistake is to misplace the curly brackets, so the else-part is declared within the code block of the if-statement at the top. Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Hi Nina,
Thanks for responding.
I am using the code from lecture (I downloaded it to see what the problem is), so I shouldn’t be having problems :frowning:
The thrusting issue is fixed - I had renamed the scripts to GameDevMovement and GameDevCollision, but not renamed the GetComponent() to match. (to GetComponent()).

I am however still having problems:
a) There is a long delay from when a collision occurs, and the sound is heard.
b) The particle system for success and crash no longer works, but the system for thrusters still works.
c) The sound for rotational thrust is still a ‘buzzing’ sound compared to main thrust which sounds correct.

Code being used:

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class GameDevMovement : MonoBehaviour

{

    // PARAMETERS - for tuning, typically set in the editor

    // CACHE - e.g. references for readability or speed

    // STATE - private instance (member) variables

    [SerializeField] float mainThrust = 100f;

    [SerializeField] float rotationThrust = 20f;

    [SerializeField] AudioClip mainEngine;

    [SerializeField] ParticleSystem mainEngineParticles;

    [SerializeField] ParticleSystem leftThrusterParticles;

    [SerializeField] ParticleSystem rightThrusterParticles;

    Rigidbody rb;

    AudioSource audioSource;

    // Start is called before the first frame update

    void Start()

    {

        rb = GetComponent<Rigidbody>();

        audioSource = GetComponent<AudioSource>();

    }

    // Update is called once per frame

    void Update()

    {

        ProcessThrust();

        ProcessRotation();

    }

    void ProcessThrust()

    {

        if (Input.GetKey(KeyCode.Space))

        {

            StartThrusting();

        }

        else

        {

            StopThrusting();

        }

    }

    void ProcessRotation()

    {

        if (Input.GetKey(KeyCode.A))

        {

            RotateLeft();

        }

        else if (Input.GetKey(KeyCode.D))

        {

            RotateRight();

        }

        else

        {

            StopRotating();

        }

    }

    void StartThrusting()

    {

        float yValue = mainThrust*Time.deltaTime;

       // Debug.Log("upforce :" + yValue);

        rb.AddRelativeForce(Vector3.up * yValue);

        if (!audioSource.isPlaying)

        {

            audioSource.PlayOneShot(mainEngine);

        }

        if (!mainEngineParticles.isPlaying)

        {

            mainEngineParticles.Play();

        }

    }

    private void StopThrusting()

    {

        audioSource.Stop();

        mainEngineParticles.Stop();

    }

    private void RotateLeft()

    {

        ApplyRotation(rotationThrust);

        if (!rightThrusterParticles.isPlaying)

        {

            rightThrusterParticles.Play();

        }

    }

    private void RotateRight()

    {

        ApplyRotation(-rotationThrust);

       

        if (!leftThrusterParticles.isPlaying)

        {

            leftThrusterParticles.Play();

        }

    }

    private void StopRotating()

    {

        rightThrusterParticles.Stop();

        leftThrusterParticles.Stop();

    }

    void ApplyRotation(float rotationThisFrame)

    {

        rb.freezeRotation = true;  // freezing rotation so we can manually rotate

        transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);

        rb.freezeRotation = false;  // unfreezing rotation so the physics system can take over

         // added audiosource 9/4

        if (!audioSource.isPlaying)

        {

            audioSource.PlayOneShot(mainEngine);

        }

    }

}

using System;

using UnityEngine;

using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour

{

    [SerializeField] float levelLoadDelay = 2f;

    [SerializeField] AudioClip success;

    [SerializeField] AudioClip crash;

    [SerializeField] ParticleSystem successParticles;

    [SerializeField] ParticleSystem crashParticles;

    AudioSource audioSource;

    bool collisionDisabled = false;

    bool isTransitioning = false;

    void Start()

    {

        audioSource = GetComponent<AudioSource>();

    }

    void Update() {

        RespondToDebugKeys();

    }

   

    void RespondToDebugKeys() {

        if(Input.GetKeyDown(KeyCode.L)){

            Debug.Log("Debug L");

            LoadNextLevel();

        }

        if(Input.GetKeyDown(KeyCode.C)){

             Debug.Log("Debug C: " + collisionDisabled);

            collisionDisabled = !collisionDisabled; // toggle

        }

    }

    void OnCollisionEnter(Collision other)

    {

        if (isTransitioning || collisionDisabled) { return;}

        switch (other.gameObject.tag)

        {

            case "Start":

                break;

            case "Obstacle":

                StartCrashSequence();

                break;

            case "Finish":

                StartSuccessSequence();

                break;

            default:

                break;

        }

    }

    void StartSuccessSequence()

    {

   /*     audioSource.PlayOneShot(success);

        // todo add particle effect upon success

        successParticles.Play();

        GetComponent<GameDevMovement>().enabled = false;

        Invoke("LoadNextLevel", levelLoadDelay);

    */

        isTransitioning = true;

        audioSource.Stop();

       

        audioSource.PlayOneShot(success);

        if(!successParticles.Play()) {

            successParticles.Play();

        }

        GetComponent<GameDevMovement>().enabled = false;

        Invoke("LoadNextLevel", levelLoadDelay);

    }

    void StartCrashSequence()

    {

        audioSource.PlayOneShot(crash);

        // todo add particle effect upon crash

        crashParticles.Play();

        GetComponent<GameDevMovement>().enabled = false;

        Invoke("ReloadLevel", levelLoadDelay);

    }

    void LoadNextLevel()

    {

        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

        int nextSceneIndex = currentSceneIndex + 1;

        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)

        {

            nextSceneIndex = 0;

        }

        SceneManager.LoadScene(nextSceneIndex);

    }

    void ReloadLevel()

    {

        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

        SceneManager.LoadScene(currentSceneIndex);

    }

}

Thanks.

Regarding the sound, your code looks fine so far. You have if (!audioSource.isPlaying), which is supposed to prevent the audio source from playing multiple sounds simultaneously. And the StopThrusting() method call is in an else-block, so the audio source won’t get stopped and started every other frame. That’s the logic flow we wanted to implement. However, since there is an issue with the sound, don’t rely on what we wanted to do but check if we were successful. Use Debug.Logs to see if the code gets executed as expected.

Regarding the delay, the game window is just a laggy preview. If you cannot spot any problem in your project, I would suggest to build your game and test the build. If there is no delay, there very likely is no issue in your actual game in this respect.


Where is the rotational thrust sound? From what I see in your code, the only AudioClip variable in your GameDevMovement class is mainEngine.


Add Debug.Logs, and also check your console. Maybe there is an error message which gives us a hint what might be wrong.

Privacy & Terms