Hello everyone ![]()
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! ![]()