Guest User

Untitled

a guest
Jun 3rd, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using MoreMountains.NiceVibrations;
  5. //using XInputDotNetPure;
  6. public class Damage : MonoBehaviour
  7. {
  8.     public Rigidbody rb;
  9.     public WheelCollider[] wheel;
  10.     float speed;
  11.     float combine;
  12.     JointSpring ss;
  13.     public AudioSource audio;
  14.     public AudioClip ac;
  15.     public ParticleSystem sparks;
  16.     public short sparkCount;
  17.     //PlayerIndex playerIndex;
  18.     //GamePadState state;
  19.     //GamePadState prevState;
  20.     public bool grounded = false;
  21.     public bool ramp = false;
  22.     //public Material materialColor;
  23.     float initialMass;
  24.     public GameObject p;
  25.     public Color newColor;
  26.     public Material mat;
  27.     float initTorque;
  28.     float initMaxSpeed;
  29.     public float stabilizeSpeed;
  30.     Vector3 test;
  31.    
  32.     void Start()
  33.     {
  34.         ss = wheel[0].suspensionSpring;
  35.         initialMass = rb.mass;
  36.         adjust();
  37.         FindObjectOfType<GameManager>().Mass(rb.mass);
  38.        
  39.         for (int i = 0; i < wheel.Length; i++)
  40.         {
  41.             wheel[i].ConfigureVehicleSubsteps(500, 1200, 1500);
  42.         }
  43.     }
  44.  
  45.     void Update()
  46.     {
  47.  
  48.         for (int i = 0; i < wheel.Length; i++)
  49.         {
  50.             if (wheel[i].isGrounded == false)
  51.             {
  52.                 grounded = false;
  53.                 ramp = false;
  54.                 // rb.constraints = RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
  55.                 //transform.rotation = Quaternion.LerpUnclamped(transform.rotation, Quaternion.Euler(transform.eulerAngles.x,0,0), Time.deltaTime * stabilizeSpeed);
  56.  
  57.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(transform.eulerAngles.x, 0, 0), Time.deltaTime);
  58.  
  59.                
  60.  
  61.                 // transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.x, 0, 0), Time.deltaTime * stabilizeSpeed);
  62.  
  63.                 //var angles = transform.eulerAngles;
  64.                 //angles.y = Mathf.MoveTowardsAngle(angles.y, 0, stabilizeSpeed * Time.deltaTime);
  65.                 //angles.z = Mathf.MoveTowardsAngle(angles.z, 0, stabilizeSpeed * Time.deltaTime);
  66.                 //transform.eulerAngles = angles;
  67.  
  68.  
  69.             }
  70.  
  71.             if (grounded == false)
  72.             {
  73.                 if (wheel[i].isGrounded == true)
  74.                 {
  75.                     grounded = true;
  76.                     if (wheel[i].GetGroundHit(out WheelHit hit))
  77.                     {
  78.                         if (hit.collider.transform.gameObject.CompareTag("Ramp"))
  79.                         {
  80.                             print("yo");
  81.                             ramp = true;
  82.                         }
  83.                     }
  84.                     else
  85.                     {
  86.                         ramp = false;
  87.                     }
  88.  
  89.                 }
  90.             }      
  91.  
  92.         }
  93.         if (grounded == true)
  94.         {
  95.             FindObjectOfType<GameManager>().s = rb.position.z;
  96.         }
  97.     }
  98.     private void OnCollisionEnter(Collision col)
  99.     {
  100.         combine = Mathf.Abs(col.impactForceSum.x) + Mathf.Abs(col.impactForceSum.y) + Mathf.Abs(col.impactForceSum.z); replace
  101.         //print("combine: " + combine);
  102.         if (rb.mass / ( 1 + (combine/1000) ) > 10)
  103.         {
  104.             rb.mass = rb.mass / (1 + (combine/1000) );
  105.         }
  106.         else
  107.         {
  108.             rb.mass = 10;
  109.         }
  110.  
  111.         adjust();
  112.         //AndroidManager.HapticFeedback();
  113.  
  114.         FindObjectOfType<GameManager>().Mass(rb.mass);
  115.        
  116.  
  117.         audio.PlayOneShot(ac,combine/200);
  118.         MMVibrationManager.TransientHaptic(combine / 200,0.1f,true,this);
  119.         sparkCount = (short)(combine/8);
  120.         Instantiate(sparks, col.contacts[0].point, Quaternion.identity);
  121.        
  122.         print(combine/200);
  123.         //MMVibrationManager.Haptic(HapticTypes.MediumImpact,false,false,this);
  124.         //audio.pitch = Random.Range(0.8f, 1.2f);
  125.         //Handheld.Vibrate();
  126.         //StartCoroutine(Vibrate());
  127.     }
  128.  
  129.    
  130.  
  131.     void adjust()
  132.     {
  133.         FindObjectOfType<AllWheelDrive>().torque = 12 * rb.mass * FindObjectOfType<GameManager>().speedMultiplier;
  134.  
  135.         ss.spring = 200 * rb.mass;
  136.         ss.damper = 4 * rb.mass;
  137.         for (int i = 0; i < wheel.Length; i++)
  138.         {
  139.             wheel[i].suspensionSpring = ss;
  140.             wheel[i].mass = 0.01f * rb.mass;
  141.             //mesh = p.GetComponent<MeshRenderer>();
  142.            
  143.         }
  144.         newColor = new Color((initialMass - rb.mass) / initialMass, rb.mass / initialMass, 0);
  145.         mat.color = newColor;
  146.     }
  147.  
  148.     //IEnumerator Vibrate()
  149.     //{
  150.     //    GamePad.SetVibration(playerIndex, combine / 100, combine / 100);
  151.     //    yield return new WaitForSeconds(0.1f);
  152.     //    GamePad.SetVibration(playerIndex, 0, 0);
  153.     //}
  154. }
Advertisement
Add Comment
Please, Sign In to add comment