Conserve some of Rigidbody's speed when turning.
Hello all, Im trying to make a top down spaceship game. My code is a slightly modified version of this code: http://wiki.unity3d.com/index.php?title=ShipControls . The problem im having is that it works wonderfully with small ships like fighthers since they have a lot of thrust so they can change direction fast.
However i want bigger and more heavy ships to gradually accelerate and take them several seconds until they reach max speed. The problem comes when i have to turn with them, they just keep drifting in the previous direction way too long. Ideally I'd like this big ships to behave more like actual sea ships than spaceships where if they turn, they loose some speed but keep most of it.
Im not very experienced with physics so i have no idea how to handle it. If there is any way to get a similar result to what im doing WITHOUT physics I'd be very grateful since as far as i know, physics are very bad for smartphones right?
This is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// Put this on a rigidbody object and instantly
// have 2D spaceship controls like OverWhelmed Arena
// that you can tweak to your heart's content.
[RequireComponent (typeof (Rigidbody))]
public class ShipControls : MonoBehaviour
{
public float hoverHeight = 3F;
public float hoverHeightStrictness = 1F;
public float forwardThrust = 5000F;
public float backwardThrust = 2500F;
public float bankAmount = 0.1F;
public float bankSpeed = 0.2F;
public Vector3 bankAxis = new Vector3(-1F, 0F, 0F);
public float turnSpeed = 8000F;
public float maxSpeed;
public Text textoMotores;
public Slider sliderMotores;
public Text textoSpeed;
public Vector3 forwardDirection = new Vector3(1F, 0F, 0F);
public float mass = 5F;
// positional drag
public float sqrdSpeedThresholdForDrag = 25F; //VELOCIDAD A LA QUE SE EMPEZARA A APLICAR EL FAST DRAG PARA
//PONER UNA VELOC MAX
public float superDrag = 2F; //DRAG CUANDO NO SE PULSA NADA
public float fastDrag = 0.5F;
public float slowDrag = 0.01F;
// angular drag
public float sqrdAngularSpeedThresholdForDrag = 5F; //VELOCIDAD DE GIRO A LA QUE SE ACTIVARA FAST DRAG
public float superADrag = 32F;
public float fastADrag = 16F;
public float slowADrag = 0.1F;
public bool playerControl = true;
float bank = 0F;
void SetPlayerControl(bool control)
{
playerControl = control;
}
void Start()
{
GetComponent<Rigidbody>().mass = mass;
}
void FixedUpdate()
{
textoSpeed.text = GetComponent<Rigidbody> ().velocity.magnitude.ToString ();
sqrdSpeedThresholdForDrag = thrust * maxSpeed;
if (Mathf.Abs(thrust) > 0.01F)
{
if (GetComponent<Rigidbody>().velocity.magnitude > sqrdSpeedThresholdForDrag)
GetComponent<Rigidbody>().drag = fastDrag;
else
GetComponent<Rigidbody>().drag = slowDrag;
}
else
GetComponent<Rigidbody>().drag = superDrag;
if (Mathf.Abs(turn) > 0.01F)
{
if (GetComponent<Rigidbody>().angularVelocity.sqrMagnitude > sqrdAngularSpeedThresholdForDrag)
GetComponent<Rigidbody>().angularDrag = fastADrag;
else
GetComponent<Rigidbody>().angularDrag = slowADrag;
}
else
GetComponent<Rigidbody>().angularDrag = superADrag;
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, hoverHeight, transform.position.z), hoverHeightStrictness);
float amountToBank = GetComponent<Rigidbody>().angularVelocity.y * bankAmount;
bank = Mathf.Lerp(bank, amountToBank, bankSpeed);
Vector3 rotation = transform.rotation.eulerAngles;
rotation *= Mathf.Deg2Rad;
rotation.x = 0F;
rotation.z = 0F;
rotation += bankAxis * bank;
//transform.rotation = Quaternion.EulerAngles(rotation);
rotation *= Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(rotation);
}
float thrust = 0F;
float turn = 0F;
void Thrust(float t)
{
thrust = Mathf.Clamp(t, -1F, 1F);
}
void Turn(float t)
{
turn = Mathf.Clamp(t, -1F, 1F) * turnSpeed;
}
bool thrustGlowOn = false;
void Update ()
{
Debug.Log (this.gameObject.name + GetComponent<Rigidbody> ().velocity.magnitude);
float theThrust = thrust;
if (playerControl)
{
if (Input.GetKey (KeyCode.W)) {
thrust +=0.1f;
if (thrust>1){
thrust = 1;
}
}
if (Input.GetKey (KeyCode.S)) {
thrust -=0.1f;
if (thrust<-1){
thrust = -1;
}
}
if (Input.GetKey (KeyCode.X)) {
thrust =0f;
}
sliderMotores.value = thrust * 100;
textoMotores.text = (Mathf.Round(thrust * 100)).ToString();
turn = Input.GetAxis("Horizontal") * turnSpeed;
}
if (thrust > 0F)
{
theThrust *= forwardThrust;
if (!thrustGlowOn)
{
thrustGlowOn = !thrustGlowOn;
BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
}
}
else
{
theThrust *= backwardThrust;
if (thrustGlowOn)
{
thrustGlowOn = !thrustGlowOn;
BroadcastMessage("SetThrustGlow", thrustGlowOn, SendMessageOptions.DontRequireReceiver);
}
}
GetComponent<Rigidbody>().AddRelativeTorque(Vector3.up * turn * Time.deltaTime);
GetComponent<Rigidbody>().AddRelativeForce(forwardDirection * theThrust * Time.deltaTime);
}
}