Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class MovementStuff : MonoBehaviour
- {
- public float steeringSensitivity = 200;
- public float motorPower = 3;
- public float brakePower = 3;
- public float groundAngularDrag = 1;
- public float airAngularDrag = 10;
- //public float thrustMulti = 1;
- public float maxExtraThrust;
- public Transform centerofmass;
- public Text debugtext;
- Transform Rig;
- Rigidbody Body;
- public Transform Craft;
- public Transform CamOffset;
- public GameObject Head;
- public InputStuff leftController;
- public InputStuff rightController;
- //public TrackedPoseDriver HMD;
- public WheelCollider WLF, WRF, WLB, WRB;
- public Transform leftAnchor, rightAnchor;
- public float accelerationPower;
- public float decelerationPower;
- public float torquePower;
- public float yawPower;
- public float groundDrag = 5;
- Vector3 RigIntendedVelocity;
- Vector3 RigCurrentVelocity;
- Vector3 RigIntendedRotation;
- public Transform UI_horizontal, UI_vertical, UI_roll, UI_movedot;
- float torque_pitch, torque_yaw, torque_roll;
- float thrust;
- float gearReverse = -1;
- Vector3 quadMoveLeft, quadMoveRight;
- Vector3 thrustup;
- bool leftStickHolding = false, rightStickHolding = false;
- public LineRenderer leftLine, rightLine;
- [SerializeField] AnimationCurve torqueCurve;
- float actualTorque;
- [SerializeField] AnimationCurve rotationCurve;
- float actualThrust;
- Vector3 localVelocity;
- // Auto brake the wheels if flying.
- float autoBrake = 1;
- float currentSteeringAngle;
- public Transform _barrell;
- public GameObject beam;
- public Rigidbody _bullet;
- public float bulletSpeed = 100;
- AudioSource audioSource;
- [SerializeField] float audioPitch = 1;
- [SerializeField] float audioVolume = 1;
- // TODO: Rewrite the script so that things are in their own methods and Update only runs those methods.
- // The methods themselves have all the stuff in them. Right now it's all in Update and that's messy.
- void Update(){
- // Reset level if controller is below the head
- // Change to other level if controller above the head
- // I know this is silly
- if (rightController.input_primaryPress.action.triggered) {
- if (rightController.transform.position.y < Head.transform.position.y) {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
- } else if (rightController.transform.position.y > Head.transform.position.y) {
- if (SceneManager.GetActiveScene().buildIndex == 0) {
- SceneManager.LoadScene(1, LoadSceneMode.Single);
- } else if (SceneManager.GetActiveScene().buildIndex == 1) {
- SceneManager.LoadScene(0, LoadSceneMode.Single);
- }
- }
- }
- //if (rightController.input_primaryPress.action.triggered) SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
- // if (leftController.triggerAnalog > 0.1f) {
- // beam.SetActive(true);
- // } else {
- // beam.SetActive(false);
- // }
- if (leftController.triggerAnalog == 1 && rightController.triggerAnalog == 1) {
- Rigidbody thrown = Instantiate(_bullet, _barrell.transform.position, _barrell.rotation);
- thrown.velocity = _barrell.forward * bulletSpeed;
- }
- if (leftController.gripForce > 0.75f && !leftStickHolding) {
- leftStickHolding = true;
- leftAnchor.position = leftController.PoseCube.position;
- leftLine.positionCount = 2;
- leftAnchor.gameObject.SetActive(true);
- }
- if (leftController.gripForce > 0.75f && leftStickHolding) {
- leftAnchor.position = leftController.PoseCube.position;
- }
- if (leftController.secondaryPress == 1) {
- CamOffset.localPosition += Vector3.Scale(-leftController.velocity, new Vector3(0.02f, 0.02f, 0.02f));
- PlayerPrefs.SetFloat("sakkevrx", CamOffset.localPosition.x);
- PlayerPrefs.SetFloat("sakkevry", CamOffset.localPosition.y);
- PlayerPrefs.SetFloat("sakkevrz", CamOffset.localPosition.z);
- PlayerPrefs.Save();
- }
- if (rightController.gripForce > 0.75f && !rightStickHolding) {
- rightStickHolding = true;
- rightAnchor.position = rightController.PoseCube.position;
- rightAnchor.rotation = rightController.PoseCube.rotation;
- rightLine.positionCount = 2;
- rightAnchor.gameObject.SetActive(true);
- }
- if (rightController.gripForce > 0.75f && rightStickHolding) {
- rightAnchor.position = rightController.PoseCube.position;
- rightAnchor.rotation = rightController.PoseCube.rotation;
- }
- if (leftController.thumbForce > 0.1f){
- leftStickHolding = false;
- leftLine.positionCount = 1;
- quadMoveLeft = Vector3.zero;
- leftAnchor.gameObject.SetActive(false);
- }
- if (rightController.thumbForce > 0.1f) {
- rightStickHolding = false;
- rightLine.positionCount = 1;
- quadMoveRight = Vector3.zero;
- rightAnchor.gameObject.SetActive(false);
- }
- if (rightStickHolding) rightLine.SetPosition(1, rightLine.transform.InverseTransformPoint(rightController.PoseCube.position));
- if (leftStickHolding) leftLine.SetPosition(1, leftLine.transform.InverseTransformPoint(leftController.PoseCube.position));
- // Virtual joystick created by looking at vectors from anchor to controller.
- if (leftStickHolding) {
- quadMoveLeft = new Vector3(
- Vector3.Dot(leftAnchor.right, leftController.PoseCube.position - leftAnchor.position),
- Vector3.Dot(leftAnchor.up, leftController.PoseCube.position - leftAnchor.position),
- Vector3.Dot(leftAnchor.forward, leftController.PoseCube.position - leftAnchor.position)
- );
- } //else { quadMoveLeft = Vector3.zero; }
- if (rightStickHolding) {
- quadMoveRight = new Vector3(
- Vector3.Dot(rightAnchor.right, rightController.PoseCube.position - rightAnchor.position),
- Vector3.Dot(rightAnchor.up, rightController.PoseCube.position - rightAnchor.position),
- Vector3.Dot(rightAnchor.forward, rightController.PoseCube.position - rightAnchor.position)
- );
- } //else { quadMoveRight = Vector3.zero; }
- //UI_horizontal.localPosition = new Vector3(0, quadMoveLeft.y, 1.2f);
- //UI_vertical.localPosition = new Vector3(quadMoveRight.x, 0, 1.2f);
- //UI_roll.localEulerAngles = new Vector3(0, 0, -quadMoveRight.x * 200);
- //UI_movedot.localPosition = new Vector3(quadMoveLeft.x, quadMoveLeft.z, 1.199f);
- torque_pitch = quadMoveLeft.z * rotationCurve.Evaluate(Mathf.Abs(quadMoveLeft.z)) * torquePower;
- torque_roll = quadMoveLeft.x * rotationCurve.Evaluate(Mathf.Abs(quadMoveLeft.x)) * -torquePower;
- torque_yaw = quadMoveRight.x * rotationCurve.Evaluate(Mathf.Abs(quadMoveRight.x)) * torquePower;
- //rotationCurve.Evaluate(x) * torquePower;
- // float currentYawAngle = Vector3.Angle(rightController.PoseCube.forward, rightAnchor.right);
- // if (rightStickHolding) {
- // torque_yaw = (90 - currentYawAngle) * yawPower;
- // } else torque_yaw = 0;
- // if (WLF.isGrounded || WRF.isGrounded || WLB.isGrounded || WRB.isGrounded) {
- // torque_yaw = 0;
- // torque_pitch *= 0.15f;
- // torque_roll *= 0.15f;
- // Body.angularDrag = groundAngularDrag;
- // } else {
- // Body.angularDrag = airAngularDrag;
- // }
- if (!leftStickHolding && !rightStickHolding) {
- Body.angularDrag = groundAngularDrag;
- // Wheel turning
- currentSteeringAngle = Vector3.Angle(rightController.transform.position - leftController.transform.position, -Rig.up);
- } else {
- Body.angularDrag = airAngularDrag;
- currentSteeringAngle = 90;
- }
- WLF.steerAngle = (90 - currentSteeringAngle) * steeringSensitivity;
- WRF.steerAngle = (90 - currentSteeringAngle) * steeringSensitivity;
- // Wheel torque
- localVelocity = transform.InverseTransformVector(Body.velocity);
- actualTorque = torqueCurve.Evaluate(localVelocity.z);
- // Reverse
- if (rightController.move2D.y < -0.1f) gearReverse = -1;
- else gearReverse = 1;
- WLF.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
- WRF.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
- WLB.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
- WRB.motorTorque = rightController.triggerAnalog * actualTorque * motorPower * gearReverse;
- //debugtext.text = Vector3.Dot(Body.velocity, thrustup.normalized).ToString("f3") + "\n" + actualThrust.ToString("f3");
- // RPM's of the wheels into an array.
- // float[] rpms = { WLF.rpm, WRF.rpm, WLB.rpm, WRB.rpm };
- // Wheel brake
- WLF.brakeTorque = leftController.triggerAnalog * brakePower;
- WRF.brakeTorque = leftController.triggerAnalog * brakePower;
- WLB.brakeTorque = leftController.triggerAnalog * brakePower;
- WRB.brakeTorque = leftController.triggerAnalog * brakePower;
- // Debug display (By Tommy Evans)
- //MessageDisplay.FontSize = 30;
- //MessageDisplay.AddMessage(localVelocity.magnitude.ToString("f3"));
- // 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.
- thrustup = Rig.up * Mathf.Clamp(quadMoveLeft.y, 0, 0.2f) * 5;
- //debugtext.text = (Mathf.Clamp(quadMoveLeft.y, 0, 0.2f) * 5).ToString("f3");
- // Thrust calculation where you lose thrust power the faster you're going in the thrust direction.
- // And you get some extra thrust power if you're trying to accelerate against current velocity direction.
- actualThrust = Mathf.Clamp(accelerationPower - Vector3.Dot(Body.velocity, thrustup.normalized), 0, accelerationPower + maxExtraThrust);
- audioSource.pitch = 0.4f + Mathf.Clamp(quadMoveLeft.y, 0, 0.2f) * 5 * 1.5f;
- }
- void FixedUpdate() {
- //Body.AddForce(thrustup * ((accelerationPower) + (maxExtraThrust * thrustMulti)), ForceMode.Acceleration);
- Body.AddForce(thrustup * actualThrust, ForceMode.Acceleration);
- Body.AddRelativeTorque(torque_pitch, torque_yaw, torque_roll, ForceMode.Acceleration);
- }
- void Start(){
- Rig = GetComponent<Transform>();
- Body = GetComponent<Rigidbody>();
- Body.maxAngularVelocity = 10;
- Body.centerOfMass = centerofmass.localPosition;
- audioSource = GetComponent<AudioSource>();
- audioSource.pitch = audioPitch;
- if (PlayerPrefs.HasKey ("sakkevrx") && PlayerPrefs.HasKey ("sakkevry") && PlayerPrefs.HasKey ("sakkevrz")) {
- float x = PlayerPrefs.GetFloat("sakkevrx");
- float y = PlayerPrefs.GetFloat("sakkevry");
- float z = PlayerPrefs.GetFloat("sakkevrz");
- CamOffset.localPosition = new Vector3(x, y, z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement