Advertisement
Guest User

acceleration

a guest
Nov 23rd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ControlShip : MonoBehaviour {
  5.  
  6.     public int rotationSpeed = 75;
  7.     public int movementspeed = 10;
  8.     private int thrust = 5;
  9.  
  10.     bool isPKeyDown = false;
  11.     float acceleration = .0f;
  12.  
  13.     Vector3 previousPosition = Vector3.zero;
  14.  
  15.     Rigidbody _rigidbody;
  16.  
  17.     // Use this for initialization
  18.     void Start () {
  19.  
  20.         _rigidbody = GetComponent<Rigidbody>();
  21.         Debug.Log("Acc Speed: " + thrust);
  22.     }
  23.  
  24.     // Update is called once per frame
  25.     void Update () {
  26.  
  27.         //var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
  28.         //transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
  29.         //transform.position += transform.forward * Time.deltaTime * movementspeed; // this can break while calculations ... since its before calculating acceleration
  30.  
  31.         if (Input.GetKey(KeyCode.Z))
  32.             transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
  33.  
  34.         if ((isPKeyDown = Input.GetKey("p")))
  35.         {
  36.             float distance = Vector3.Distance(previousPosition, transform.position);
  37.             acceleration = distance / Mathf.Pow(Time.deltaTime, 2);
  38.  
  39.             _rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
  40.         }
  41.  
  42.         var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
  43.         transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
  44.         transform.position += transform.forward * Time.deltaTime * movementspeed;
  45.        
  46.         previousPosition = transform.position;
  47.     }
  48.  
  49.     void OnGUI()
  50.     {
  51.         if (isPKeyDown)
  52.         {
  53.             GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement