Hi,
I have started with unity coding a few days ago and have some experience in JavaScript as well as C#.
So I developed the following script for a spaceship but the spaceship moves very unrealistic (like it stops immediately after releasing button and is not moving really realistic. I tried to add a rigidbody and tried multiple things with add force and stuff but it didn’t really work. Can anyone tell me how I can add a realistic acceleration and movement as the space key is pressed, and a realistic braking as the space key is released?
The Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
public GameObject character;
private float turnSpeed = 80.0f;
private float acceleration = 0.5f;
private float maxSpeed = 50.0f;
void Start () {
}
void Update ()
{
if (Input.GetKey (KeyCode.Space)) {
transform.Translate (0, -acceleration * Time.deltaTime, 0);
if (acceleration < maxSpeed) {
acceleration += 1;
}
if (Input.GetKey (KeyCode.D)) {
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A)) {
transform.Rotate (Vector3.up, -turnSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.W)) {
transform.Rotate (Vector3.right, turnSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S)) {
transform.Rotate (Vector3.left, turnSpeed * Time.deltaTime);
//transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime, Space.Self);
//if (Input.GetKeyUp (KeyCode.Space)) {
//rigidbody.velocity = rigidbody.velocity * 0.9;
}
}
}