Moving Forward/Backwards with A/D

So I made my rocket look more like a ship/speeder and through this, it looks very awkward when I press A or D to rotate it. So I am thinking that instead of rotating it if I were to just move the ship forwards/backwards it would make the gameplay look more smooth.

Here is a picture of what my starship looks like:

Here is my code as of now, and it is working I just cannot figure out how to do a something transform.Rotate with position instead.

 [SerializeField] float rotateSpeed = 1f;
    [SerializeField] float thrustSpeed = 50f;
    Rigidbody starshipRigidbody;
    AudioSource starShipBooster; 

    // Start is called before the first frame update
    void Start()
    {
        starshipRigidbody = GetComponent<Rigidbody>();
        starShipBooster = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        // can thrust while rotating but cannot rotate both ways at once
        ThrustStarShip();
        RotateStarShip();
    }

    private void ThrustStarShip()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            starshipRigidbody.AddRelativeForce(Vector3.up * thrustSpeed * Time.deltaTime);
            if (!starShipBooster.isPlaying)
            {
                starShipBooster.Play();
            }

        }
        else if (Input.GetKey(KeyCode.V))
        {
            starshipRigidbody.AddRelativeForce(Vector3.down * thrustSpeed * Time.deltaTime);
        }
        else
        {
            starShipBooster.Stop();
        }
    }

    private void RotateStarShip()
    {
        float rotationThisFrame = rotateSpeed * Time.deltaTime;
        // for manual control of rotation
        starshipRigidbody.freezeRotation = true;
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }

        // enviroment control of rotation
        starshipRigidbody.freezeRotation = false;
    }
1 Like

If I understood correctly, you are trying to move the ship sideways, Right? If that’s the case, then your code already contains the answer, instead of looking at that rotation code, look at how you are moving your ship downwards and upwards, it’s the same principle but to the sides.

1 Like

Oh, I did not even think about that. Thank you.

After all of this, I am not even sure if it looks better, as it is just stale now. lol

1 Like

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

Privacy & Terms