Caminhoneiro

Delta dos irmão 2.0

Jan 5th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PhysicsManager : MonoBehaviour
  5. {
  6.     float mass = 5f;
  7.     float gravity = 9.8f;//(constante) m/s²
  8.     public float drag = 0.95f; //Descontar do Thrust
  9.  
  10.     public Rigidbody rb; // só estamos usando a velocity por enquanto. Estamos fazendo o calc de gravity na mao
  11.     public GameObject delta;//Trapezio
  12.     public Respawner rpwn;
  13.  
  14.     [Header("Divisor relativo do Thurst")]
  15.     public float boost = 0.098f; //divisor realtivo do Thurst (sim, isso é perCent) / 0.01f ~
  16.     public Quaternion lift; //theta é o ponto w direção (ou um pivot veja o que fica mais eficaz)
  17.     public float relativeThrust = 40;//e o impulso inicial
  18.     public Vector3 debugRelative;
  19.  
  20.     public AudioSource strongWind;
  21.     public AudioSource crashWing;
  22.  
  23.     public SteamVR_TrackedObject trackedObj;
  24.  
  25.     private void OnEnable()
  26.     {
  27.         rpwn = Object.FindObjectOfType<Respawner>();
  28.     }
  29.  
  30.  
  31.  
  32.     public GameObject thrustAxis;
  33.  
  34.     public float speed;
  35.     float Thrust()
  36.     {
  37.         thrustAxis.transform.position += thrustAxis.transform.forward * speed / 2;
  38.         speed -= thrustAxis.transform.forward.y * 100f;//50.0f original
  39.         if (speed < 0)
  40.             speed = 500;
  41.  
  42.         relativeThrust = speed;
  43.         return speed;
  44.     }
  45.  
  46.     Vector3 RelativeDirection()
  47.     {
  48.         Vector3 fwd = lift * Vector3.forward;
  49.         float relDirx = Mathf.Atan2(fwd.x, peso());
  50.         float relDiry = Mathf.Atan2(fwd.y, peso());
  51.         float relDirz = Mathf.Atan2(fwd.z, peso());
  52.         Vector3 torque = new Vector3(relDirx, relDiry, relDirz);
  53.  
  54.         return torque;
  55.     }
  56.     //kilograma-forca ou N (Newton)
  57.     float peso()
  58.     {
  59.         return mass * gravity;
  60.     }
  61.  
  62.     float maxDistance = 100f;
  63.     Vector3 tempVelocity;
  64.  
  65.     bool canMove;
  66.  
  67.     void Update()
  68.     {
  69.         CorrectFall();
  70.  
  71.         //Som temporario depois manda pra um mngr
  72.         if (relativeThrust > 10000)
  73.             strongWind.volume += 0.1f * Time.deltaTime;
  74.  
  75.         if (strongWind.volume > 0.8)
  76.             strongWind.volume = 0.8f;
  77.  
  78.         if (Input.GetMouseButton(0))
  79.             canMove = !canMove;
  80.     }
  81.  
  82.     void FixedUpdate()
  83.     {
  84.         if (delta != null)
  85.             lift = delta.transform.rotation;
  86.         else
  87.             return;
  88.  
  89.         rb.velocity = RelativeDirection() * Thrust();
  90.         debugRelative = rb.velocity;
  91.     }
  92.  
  93.     /// <summary>
  94.     /// Condição para saída do terreno
  95.     /// </summary>
  96.     void CorrectFall()
  97.     {
  98.         if(Terrain.activeTerrain == true)
  99.         {
  100.             float terrainHeightWhereWeAre = Terrain.activeTerrain.SampleHeight(transform.position);
  101.  
  102.             if (terrainHeightWhereWeAre > transform.position.y)
  103.             {
  104.                 transform.position = new Vector3(transform.position.x,
  105.                                                  terrainHeightWhereWeAre,
  106.                                                  transform.position.z);
  107.  
  108.                 if (rpwn != null)
  109.                     rpwn.Continue();
  110.                 crashWing.Play();
  111.                 //EventManager.RespawnPlayer();
  112.  
  113.             }
  114.             else
  115.             {
  116.                 //Faça fitas
  117.             }
  118.  
  119.         }
  120.     }
  121.  
  122.     void OnTriggerStay(Collider other)
  123.     {
  124.         if(other.gameObject.tag == "Boundary")
  125.         {
  126.             if(rpwn != null)
  127.                 rpwn.Continue();
  128.         }
  129.        
  130.         //HandleEventType();
  131.     }
  132.  
  133.     public GameObject leaves;
  134.     void OnTriggerEnter(Collider other)
  135.     {
  136.         if (other.tag == "tree")
  137.         {
  138.             leaves.SetActive(true);
  139.             StartCoroutine(SpawnLeaves());
  140.         }
  141.     }
  142.  
  143.     IEnumerator SpawnLeaves()
  144.     {
  145.         yield return new WaitForSeconds(3f);
  146.         leaves.SetActive(false);
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment