Advertisement
mew_fi

flyingandstuff

Jan 27th, 2022
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6.  
  7. public class MovementStuff : MonoBehaviour
  8. {  
  9.     public float steeringSensitivity = 200;
  10.     public float motorPower = 3;
  11.     public float brakePower = 3;
  12.     public float groundAngularDrag = 1;
  13.     public float airAngularDrag = 10;
  14.     //public float thrustMulti = 1;
  15.     public float maxExtraThrust;
  16.    
  17.     public Transform centerofmass;
  18.     public Text debugtext;
  19.  
  20.     Transform Rig;
  21.     Rigidbody Body;
  22.     public Transform Craft;
  23.     public Transform CamOffset;
  24.     public GameObject Head;
  25.     public InputStuff leftController;
  26.     public InputStuff rightController;
  27.     //public TrackedPoseDriver HMD;
  28.  
  29.     public WheelCollider WLF, WRF, WLB, WRB;
  30.  
  31.     public Transform leftAnchor, rightAnchor;
  32.  
  33.     public float accelerationPower;
  34.     public float decelerationPower;
  35.     public float torquePower;
  36.     public float yawPower;
  37.     public float groundDrag = 5;
  38.    
  39.     Vector3 RigIntendedVelocity;
  40.     Vector3 RigCurrentVelocity;
  41.     Vector3 RigIntendedRotation;
  42.  
  43.     public Transform UI_horizontal, UI_vertical, UI_roll, UI_movedot;
  44.  
  45.     float torque_pitch, torque_yaw, torque_roll;
  46.     float thrust;
  47.     float gearReverse = -1;
  48.  
  49.     Vector3 quadMoveLeft, quadMoveRight;
  50.     Vector3 thrustup;
  51.  
  52.     bool leftStickHolding = false, rightStickHolding = false;
  53.  
  54.     public LineRenderer leftLine, rightLine;
  55.  
  56.  
  57.     [SerializeField] AnimationCurve torqueCurve;
  58.     float actualTorque;
  59.     [SerializeField] AnimationCurve rotationCurve;
  60.     float actualThrust;
  61.     Vector3 localVelocity;
  62.  
  63.     // Auto brake the wheels if flying.
  64.     float autoBrake = 1;
  65.  
  66.     float currentSteeringAngle;
  67.  
  68.     public Transform _barrell;
  69.     public GameObject beam;
  70.     public Rigidbody _bullet;
  71.     public float bulletSpeed = 100;
  72.  
  73.     AudioSource audioSource;
  74.     [SerializeField] float audioPitch = 1;
  75.     [SerializeField] float audioVolume = 1;
  76.  
  77.  
  78.     // TODO: Rewrite the script so that things are in their own methods and Update only runs those methods.
  79.     // The methods themselves have all the stuff in them. Right now it's all in Update and that's messy.
  80.  
  81.     void Update(){
  82.         // Reset level if controller is below the head
  83.         // Change to other level if controller above the head
  84.         // I know this is silly
  85.         if (rightController.input_primaryPress.action.triggered) {
  86.             if (rightController.transform.position.y < Head.transform.position.y) {
  87.                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
  88.             } else if (rightController.transform.position.y > Head.transform.position.y) {
  89.                 if (SceneManager.GetActiveScene().buildIndex == 0) {
  90.                     SceneManager.LoadScene(1, LoadSceneMode.Single);
  91.                 } else if (SceneManager.GetActiveScene().buildIndex == 1) {
  92.                     SceneManager.LoadScene(0, LoadSceneMode.Single);
  93.                 }
  94.             }
  95.         }
  96.  
  97.         //if (rightController.input_primaryPress.action.triggered) SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
  98.        
  99.         // if (leftController.triggerAnalog > 0.1f) {
  100.         //     beam.SetActive(true);
  101.         // } else {
  102.         //     beam.SetActive(false);
  103.         // }
  104.  
  105.         if (leftController.triggerAnalog == 1 && rightController.triggerAnalog == 1) {
  106.             Rigidbody thrown = Instantiate(_bullet, _barrell.transform.position, _barrell.rotation);
  107.             thrown.velocity = _barrell.forward * bulletSpeed;
  108.         }
  109.  
  110.         if (leftController.gripForce > 0.75f && !leftStickHolding) {
  111.             leftStickHolding = true;
  112.             leftAnchor.position = leftController.PoseCube.position;
  113.             leftLine.positionCount = 2;
  114.             leftAnchor.gameObject.SetActive(true);
  115.         }
  116.         if (leftController.gripForce > 0.75f && leftStickHolding) {
  117.             leftAnchor.position = leftController.PoseCube.position;
  118.         }
  119.  
  120.         if (leftController.secondaryPress == 1) {
  121.             CamOffset.localPosition += Vector3.Scale(-leftController.velocity, new Vector3(0.02f, 0.02f, 0.02f));
  122.             PlayerPrefs.SetFloat("sakkevrx", CamOffset.localPosition.x);
  123.             PlayerPrefs.SetFloat("sakkevry", CamOffset.localPosition.y);
  124.             PlayerPrefs.SetFloat("sakkevrz", CamOffset.localPosition.z);
  125.             PlayerPrefs.Save();
  126.         }
  127.  
  128.         if (rightController.gripForce > 0.75f && !rightStickHolding) {
  129.             rightStickHolding = true;
  130.             rightAnchor.position = rightController.PoseCube.position;
  131.             rightAnchor.rotation = rightController.PoseCube.rotation;
  132.             rightLine.positionCount = 2;
  133.             rightAnchor.gameObject.SetActive(true);
  134.         }
  135.  
  136.         if (rightController.gripForce > 0.75f && rightStickHolding) {
  137.             rightAnchor.position = rightController.PoseCube.position;
  138.             rightAnchor.rotation = rightController.PoseCube.rotation;
  139.         }
  140.  
  141.         if (leftController.thumbForce > 0.1f){
  142.             leftStickHolding = false;
  143.             leftLine.positionCount = 1;
  144.             quadMoveLeft = Vector3.zero;
  145.             leftAnchor.gameObject.SetActive(false);
  146.         }
  147.            
  148.         if (rightController.thumbForce > 0.1f) {
  149.             rightStickHolding = false;
  150.             rightLine.positionCount = 1;
  151.             quadMoveRight = Vector3.zero;
  152.             rightAnchor.gameObject.SetActive(false);
  153.         }
  154.  
  155.         if (rightStickHolding) rightLine.SetPosition(1, rightLine.transform.InverseTransformPoint(rightController.PoseCube.position));
  156.         if (leftStickHolding) leftLine.SetPosition(1, leftLine.transform.InverseTransformPoint(leftController.PoseCube.position));
  157.  
  158.  
  159.         // Virtual joystick created by looking at vectors from anchor to controller.
  160.         if (leftStickHolding) {
  161.             quadMoveLeft = new Vector3(
  162.                     Vector3.Dot(leftAnchor.right, leftController.PoseCube.position - leftAnchor.position),
  163.                     Vector3.Dot(leftAnchor.up, leftController.PoseCube.position - leftAnchor.position),
  164.                     Vector3.Dot(leftAnchor.forward, leftController.PoseCube.position - leftAnchor.position)
  165.                     );
  166.         } //else { quadMoveLeft = Vector3.zero; }
  167.        
  168.         if (rightStickHolding) {
  169.             quadMoveRight = new Vector3(
  170.                     Vector3.Dot(rightAnchor.right, rightController.PoseCube.position - rightAnchor.position),
  171.                     Vector3.Dot(rightAnchor.up, rightController.PoseCube.position - rightAnchor.position),
  172.                     Vector3.Dot(rightAnchor.forward, rightController.PoseCube.position - rightAnchor.position)
  173.                     );
  174.         } //else { quadMoveRight = Vector3.zero; }
  175.  
  176.  
  177.        
  178.  
  179.         //UI_horizontal.localPosition = new Vector3(0, quadMoveLeft.y, 1.2f);
  180.         //UI_vertical.localPosition = new Vector3(quadMoveRight.x, 0, 1.2f);
  181.         //UI_roll.localEulerAngles = new Vector3(0, 0, -quadMoveRight.x * 200);
  182.         //UI_movedot.localPosition = new Vector3(quadMoveLeft.x, quadMoveLeft.z, 1.199f);
  183.  
  184.         torque_pitch = quadMoveLeft.z * rotationCurve.Evaluate(Mathf.Abs(quadMoveLeft.z)) * torquePower;
  185.         torque_roll  = quadMoveLeft.x * rotationCurve.Evaluate(Mathf.Abs(quadMoveLeft.x)) * -torquePower;
  186.         torque_yaw  = quadMoveRight.x * rotationCurve.Evaluate(Mathf.Abs(quadMoveRight.x)) * torquePower;
  187.        
  188.         //rotationCurve.Evaluate(x) * torquePower;
  189.  
  190.        
  191.         // float currentYawAngle = Vector3.Angle(rightController.PoseCube.forward, rightAnchor.right);
  192.         // if (rightStickHolding) {
  193.         //     torque_yaw = (90 - currentYawAngle) * yawPower;
  194.         // } else torque_yaw = 0;
  195.        
  196.         // if (WLF.isGrounded || WRF.isGrounded || WLB.isGrounded || WRB.isGrounded) {
  197.         //     torque_yaw = 0;
  198.         //     torque_pitch *= 0.15f;
  199.         //     torque_roll *= 0.15f;
  200.         //     Body.angularDrag = groundAngularDrag;
  201.         // } else {
  202.         //     Body.angularDrag = airAngularDrag;
  203.         // }
  204.        
  205.         if (!leftStickHolding && !rightStickHolding) {
  206.             Body.angularDrag = groundAngularDrag;
  207.             // Wheel turning
  208.             currentSteeringAngle = Vector3.Angle(rightController.transform.position - leftController.transform.position, -Rig.up);
  209.         } else {
  210.             Body.angularDrag = airAngularDrag;
  211.             currentSteeringAngle = 90;
  212.         }
  213.  
  214.         WLF.steerAngle = (90 - currentSteeringAngle) * steeringSensitivity;
  215.         WRF.steerAngle = (90 - currentSteeringAngle) * steeringSensitivity;
  216.        
  217.         // Wheel torque
  218.         localVelocity = transform.InverseTransformVector(Body.velocity);
  219.         actualTorque = torqueCurve.Evaluate(localVelocity.z);
  220.  
  221.         // Reverse
  222.         if (rightController.move2D.y < -0.1f) gearReverse = -1;
  223.         else gearReverse = 1;
  224.  
  225.         WLF.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
  226.         WRF.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
  227.         WLB.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
  228.         WRB.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
  229.  
  230.         //debugtext.text = Vector3.Dot(Body.velocity, thrustup.normalized).ToString("f3") + "\n" + actualThrust.ToString("f3");
  231.  
  232.         // RPM's of the wheels into an array.
  233.         // float[] rpms = { WLF.rpm, WRF.rpm, WLB.rpm, WRB.rpm };
  234.  
  235.  
  236.  
  237.         // Wheel brake
  238.         WLF.brakeTorque = leftController.triggerAnalog * brakePower;
  239.         WRF.brakeTorque = leftController.triggerAnalog * brakePower;
  240.         WLB.brakeTorque = leftController.triggerAnalog * brakePower;
  241.         WRB.brakeTorque = leftController.triggerAnalog * brakePower;
  242.  
  243.         // Debug display (By Tommy Evans)        
  244.         //MessageDisplay.FontSize = 30;
  245.         //MessageDisplay.AddMessage(localVelocity.magnitude.ToString("f3"));
  246.  
  247.         // Thrust vector is up in Rig space, max distance from anchor is 0.2 meters. Value multiplied by 5 so it maxes out at 1. Can't go below 0.
  248.         thrustup = Rig.up * Mathf.Clamp(quadMoveLeft.y, 0, 0.2f) * 5;
  249.         //debugtext.text = (Mathf.Clamp(quadMoveLeft.y, 0, 0.2f) * 5).ToString("f3");
  250.  
  251.         // Thrust calculation where you lose thrust power the faster you're going in the thrust direction.
  252.         // And you get some extra thrust power if you're trying to accelerate against current velocity direction.
  253.         actualThrust = Mathf.Clamp(accelerationPower - Vector3.Dot(Body.velocity, thrustup.normalized), 0, accelerationPower + maxExtraThrust);
  254.         audioSource.pitch = 0.4f + Mathf.Clamp(quadMoveLeft.y, 0, 0.2f) * 5 * 1.5f;
  255.     }
  256.  
  257.     void FixedUpdate() {
  258.         //Body.AddForce(thrustup * ((accelerationPower) + (maxExtraThrust * thrustMulti)), ForceMode.Acceleration);
  259.         Body.AddForce(thrustup * actualThrust, ForceMode.Acceleration);
  260.         Body.AddRelativeTorque(torque_pitch, torque_yaw, torque_roll, ForceMode.Acceleration);
  261.     }
  262.  
  263.     void Start(){
  264.         Rig = GetComponent<Transform>();
  265.         Body = GetComponent<Rigidbody>();
  266.         Body.maxAngularVelocity = 10;
  267.         Body.centerOfMass = centerofmass.localPosition;
  268.  
  269.         audioSource = GetComponent<AudioSource>();
  270.         audioSource.pitch = audioPitch;
  271.  
  272.  
  273.         if (PlayerPrefs.HasKey ("sakkevrx") && PlayerPrefs.HasKey ("sakkevry") && PlayerPrefs.HasKey ("sakkevrz")) {
  274.  
  275.              float x = PlayerPrefs.GetFloat("sakkevrx");
  276.              float y = PlayerPrefs.GetFloat("sakkevry");
  277.              float z = PlayerPrefs.GetFloat("sakkevrz");
  278.              CamOffset.localPosition = new Vector3(x, y, z);
  279.              
  280.  
  281.          }
  282.  
  283.     }
  284. }
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement