ShipMovement script using world coordinates and not local

Hello everyone :slight_smile:

Over the weekend I wrote this Space Ship Movement script for my game. It works but once my ship starts rotating, the controls change.
I have set everything that adds force to the Rigidbody as AddRelativeForce, but it still seems to be using some world coordinates.

Here is the script I wrote. Keep in mind, this is intended for a space game in 0G.

ShipController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShipController : MonoBehaviour {

        public float thrust;
        public float torque;
        public Rigidbody rb;
        public ParticleSystem ThrustParticles;
        public ParticleSystem RightThrust;
        public ParticleSystem LeftThrust;
        public Light ThrusterLight;
        public Slider FuelSlider;
        public float FuelAmount;
        public bool ThrustOn = false;
        public Camera ShipCamera;

        void Start() 
        {
            rb = GetComponent<Rigidbody>();
            ThrusterLight.enabled = false;
            FuelSlider.value = 10000f;
        }

        void Update() 
    {
       

        if (Input.GetKeyDown(KeyCode.F)) {
            FuelSlider.value = 10000f;
        }

        if (Input.GetKeyDown (KeyCode.A)) {
            RightThrust.Play ();
        }            

        if (Input.GetKeyUp(KeyCode.A)) {
            RightThrust.Stop ();
        }

        if (Input.GetKeyDown (KeyCode.D)) {
            LeftThrust.Play ();
        }

        if (Input.GetKeyUp(KeyCode.D)){
            LeftThrust.Stop ();
        }
           

        if (Input.GetKey(KeyCode.Space ))
        {
            if (FuelSlider.value == 0) {
                return;

            } else
                rb.AddRelativeForce (0, 0, thrust, ForceMode.Acceleration);
            FuelSlider.value -= 1;
                       
        }

        if (Input.GetKeyDown(KeyCode.Space)) 
        {
            if (FuelSlider.value == 0) {
                return;
            } else
                ThrustParticles.Play ();
            ThrusterLight.enabled = true;
            ThrustOn = true;

        } 
        if (Input.GetKeyUp (KeyCode.Space))
        {
           
            ThrustParticles.Stop ();
            ThrusterLight.enabled = false;
            ThrustOn = false;
        }

        if (FuelSlider.value == 0) 
        {
            return;
        }
            else
            {
                float turnH = Input.GetAxis("Horizontal");
            rb.AddRelativeTorque(transform.up * torque * turnH);

                float turnV = Input.GetAxis("Vertical");
                rb.AddRelativeTorque(transform.right * torque * turnV);

            /*
            * if (Input.GetKey(KeyCode.Q)) {
                rb.AddRelativeForce (transform.rotation);
            }
            */
            }


        if (FuelSlider.value == 0) {
            ThrustParticles.Stop ();
            ThrusterLight.enabled = false;

        }
    }
}

Any help would be very appreciated! :slight_smile:

It’s changing because you’re using transform.up and transform.right, which are changing as you rotate. Use Vector3.up and Vector3.right since AddRelativeTorque does the conversion for you.

1 Like

Works perfectly :slight_smile: Thank you!