Advertisement
Guest User

Spacecraft

a guest
Feb 12th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Spacecraft : MonoBehaviour {
  5.  
  6.     public GameObject ForwardEngine;
  7.     public GameObject BackwardEngine;
  8.     public GameObject PlusYRotationEngine;
  9.     public GameObject MinusYRotationEngine;
  10.     public GameObject PlusXRotationEngine;
  11.     public GameObject MinusXRotationEngine;
  12.  
  13.     public float Damage = 30f;
  14.     public float impulse = 3f;
  15.     public float MaxSpeed = 300f;
  16.     public float MaxAngularVielocity = 20f;
  17.     public float Fuel = 100f;
  18.     public float FuelConsum = 0.01f;
  19.  
  20.     private Vector3 CurrentVelocity;
  21.     private Vector3 NewVelocity;
  22.     private Vector3 CurrentTorque;
  23.     private Vector3 NewTorque;
  24.     private GameObject[] engines;
  25.  
  26.     void Start(){
  27.     engines = new GameObject[] { //Перечисляем все движки в массив, чтобы, включать/выключать их все.
  28.             ForwardEngine,
  29.             BackwardEngine,
  30.             PlusYRotationEngine,
  31.             MinusYRotationEngine,
  32.             PlusXRotationEngine,
  33.             MinusXRotationEngine
  34.         };
  35.         Physics.gravity = Vector3.zero;
  36.     }
  37.        
  38.         // Update is called once per frame
  39.     void Update () {
  40.  
  41.         CurrentVelocity = GetComponent<Rigidbody> ().velocity;      //      Получаем текущую скорость.
  42.         CurrentTorque = GetComponent<Rigidbody>().angularVelocity;  //      Получаем текущий момент вращения.
  43.  
  44.         if (Fuel == 0) {
  45.             TotalStop ();
  46.         }
  47.         if (Fuel != 0) {                                            //Проверяем, есть ли топливо
  48.             foreach(GameObject element in engines){                 // Если есть - включаемся.
  49.                 element.SetActive (true);
  50.             }
  51. //      Ускорение вперед
  52.             if (Input.GetKey (KeyCode.W)) {
  53.                 NewVelocity = CurrentVelocity + transform.forward * impulse*Time.deltaTime;
  54.                 EngineStart (ForwardEngine);
  55.             } else {
  56.                 EngineStop (ForwardEngine);
  57.             }
  58.  
  59.  
  60. //      Ускорение назад(торможение)
  61.             if (Input.GetKey (KeyCode.S)) {
  62.                 NewVelocity = CurrentVelocity - transform.forward * impulse*Time.deltaTime;
  63.                 EngineStart (BackwardEngine);
  64.             } else {
  65.                 EngineStop (BackwardEngine);
  66.             }
  67.  
  68. //      Создание положительного момента вращения относительно У
  69.             if (Input.GetKey (KeyCode.D)) {
  70.                 NewTorque = CurrentTorque + transform.up * impulse * 0.1f*Time.deltaTime;
  71.                 EngineStart (PlusYRotationEngine);
  72.             } else {
  73.                 EngineStop (PlusYRotationEngine);
  74.             }
  75.             //      Создание отрицательного момента вращения относительно У
  76.             if (Input.GetKey (KeyCode.A)) {
  77.                 NewTorque = CurrentTorque - transform.up * impulse * 0.1f*Time.deltaTime;
  78.                 EngineStart (MinusYRotationEngine);
  79.             } else {
  80.                 EngineStop (MinusYRotationEngine);
  81.             }
  82.  
  83.             //      Создание положительного момента вращения относительно X
  84.             if (Input.GetKey (KeyCode.Q)) {
  85.                 NewTorque = CurrentTorque + transform.right * impulse * 0.1f*Time.deltaTime;
  86.                 PlusXRotationEngine.SetActive (true);
  87.             } else {
  88.                 PlusXRotationEngine.SetActive (false);
  89.             }
  90.  
  91.             //      Создание отрицательного момента вращения относительно X
  92.             if (Input.GetKey (KeyCode.E)) {
  93.                 NewTorque = CurrentTorque - transform.right * impulse * 0.1f*Time.deltaTime;
  94.                 MinusXRotationEngine.SetActive (true);
  95.             } else {
  96.                 MinusXRotationEngine.SetActive (false);
  97.             }
  98.         }
  99.  
  100. //          Ограничение скорости, присвоение её кораблю
  101.  
  102.         NewVelocity = Vector3.ClampMagnitude (NewVelocity, MaxSpeed);
  103.         NewTorque = new Vector3 (Mathf.Clamp (NewTorque.x, -MaxAngularVielocity, MaxAngularVielocity), Mathf.Clamp (NewTorque.y, -MaxAngularVielocity, MaxAngularVielocity), Mathf.Clamp (NewTorque.z, -MaxAngularVielocity, MaxAngularVielocity));
  104.         GetComponent<Rigidbody> ().velocity = NewVelocity;
  105.         GetComponent <Rigidbody> ().angularVelocity = NewTorque;
  106.  
  107. //      Ограничение запаса топлива
  108.         Fuel = Mathf.Clamp(Fuel,0,100f);
  109.     }
  110.     public void EngineStop(GameObject engine){
  111.         ParticleSystem[] engines = engine.GetComponentsInChildren <ParticleSystem>();
  112.         foreach(ParticleSystem emiter in engines){
  113.             emiter.enableEmission = false;
  114.  
  115.         }
  116.  
  117.     }
  118.     public void EngineStart(GameObject engine){
  119.         ParticleSystem[] engines = engine.GetComponentsInChildren <ParticleSystem>();
  120.         foreach(ParticleSystem emiter in engines){
  121.             emiter.enableEmission = true;
  122.             Fuel -= (FuelConsum*Damage)*Time.deltaTime;
  123.             Fuel = Mathf.Clamp(Fuel,0,100f);
  124.         }
  125. }
  126.     public void TotalStop (){
  127.         foreach (GameObject element in engines) {
  128.             element.SetActive(false);
  129.         }
  130.     }
  131.  
  132.     public void AddFuel(float ToAdd){
  133.         Fuel = Fuel+ToAdd;
  134.     }
  135.     public void AddDamage (float damage){
  136.         Damage += damage;
  137.         Damage = Mathf.Clamp (Damage, 0, 100);
  138.         Debug.Log (Damage);
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement