How do I slow down a quickly moving spaceship
Hey, I'm trying to slow down a spaceship that's moving upwards and is connected to physics and gravity. When reaching a specific height it's supposed to slow down to a stop so it will only be able to be controlled with the keyboard. I'm relatively new to Unity and I've tried loads of stuff but it just won't work. The last thing I've tried is SmoothDamp as you can see in the code but it's not working either. I'd appreciate any help I can get.
Here's The Code: using UnityEngine; using System.Collections;
public class ShipControl : MonoBehaviour { public float speed; public float speedkey = 1.5f; private Rigidbody rb;
public float gravity = -9.81f;
public GameObject ship;
public float universeEntryHeight = 1500.0f;
private const float baseGravity = -9.81f;
private const float rEarth = 6371000;
private float rShip;
public Transform target;
public float smoothTime = 0.3F;
private float yVelocity = 0.0F;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
//befor render frame code
rShip = transform.position.y + rEarth;
gravity =
baseGravity * ((rEarth/rShip)*(rEarth/rShip));
rigidbody.AddForce(transform.right * gravity);
if (ship.transform.position.y >= universeEntryHeight) {
float newPosition = Mathf.SmoothDamp(ship.transform.position.y, target.position.y, ref yVelocity, smoothTime);
ship.transform.position = new Vector3(ship.transform.position.x, newPosition, ship.transform.position.z);
}
if (Input.GetKey("a")) {
transform.Translate(Vector3.left * Time.deltaTime);
}
if (Input.GetKey("d")) {
transform.Translate(Vector3.right * Time.deltaTime);
}
}
void FixedUpdate()
{
//Physics code
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
Comment