Advertisement
Guest User

WOTForce?.cs

a guest
Jan 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// Wheel of Triumphs wheel force
  5. /// </summary>
  6. public class WOTForce : ApplyForce
  7. {
  8.     private bool firstClick = true;
  9.  
  10.     public int first = 0;
  11.  
  12.     protected Vector3 rotationOffset;
  13.  
  14.     public float t;
  15.  
  16.     public Vector3 HandStartPos;
  17.  
  18.     Vector3 CurrentHandPos;
  19.  
  20.     [Header("VR Settings - VIVE")]
  21.     public SteamVR_TrackedObject LHand;
  22.     public SteamVR_TrackedObject RHand;
  23.     public SteamVR_Controller.Device LDevice;
  24.     public SteamVR_Controller.Device RDevice;
  25.  
  26.     public override void Start()
  27.     {
  28.         base.Start();
  29.         originalRotation = transform.rotation;
  30.  
  31.     }
  32.  
  33.     protected override void Dragging()
  34.     {
  35.         print ("Dragging");
  36.         if (!isSpinning)
  37.         {
  38.             /*RaycastHit hit;
  39.             //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  40.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  41.               if (Physics.Raycast(ray, out hit, 1000f))
  42.             {
  43.                 originalCollisionPosition = hit.point;
  44.                
  45.                 LookAtThis(hit.point);
  46.  
  47.             }*/
  48.             if (LDevice != null) {
  49.                 CurrentHandPos = LDevice.transform.pos;
  50.                 if ((HandStartPos.y - LDevice.transform.pos.y)>0.01f) {
  51.                     LookAtThis (LDevice.transform.pos);
  52.                     print (HandStartPos.y - RDevice.transform.pos.y);
  53.                 }
  54.             }
  55.             if (RDevice != null) {
  56.                 CurrentHandPos = RDevice.transform.pos;
  57.                 if ((HandStartPos.y - RDevice.transform.pos.y)>0.01f) {
  58.                     LookAtThis (RDevice.transform.pos);
  59.                     print (HandStartPos.y - RDevice.transform.pos.y);
  60.                 }
  61.             }
  62.         }
  63.         //Release (); //Whoever the hell coded this is an idiot. -SolAZDev
  64.  
  65.     }
  66.  
  67.     void Update(){
  68.         ///Working?
  69.         if (LDevice == null && LHand != null && LHand.gameObject.activeInHierarchy) {
  70.             LDevice = SteamVR_Controller.Input ((int)LHand.index);
  71.             print ("I tried to get the LDevice");
  72.         }
  73.         if (RDevice == null && RHand != null && RHand.gameObject.activeInHierarchy) {
  74.             RDevice = SteamVR_Controller.Input ((int)RHand.index);
  75.             print ("I tried to get the RDevice");
  76.         }
  77.  
  78.         //Check Press Cause ApplyForce is a little bs
  79.         if (RDevice != null && (RDevice.GetPressDown(Valve.VR.EVRButtonId.k_EButton_Grip) || RDevice.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))) {
  80.             HandStartPos = RDevice.transform.pos;
  81.             arrow.SetActive (false);
  82.             return;
  83.         }
  84.         if (LDevice != null && (LDevice.GetPressDown(Valve.VR.EVRButtonId.k_EButton_Grip) || LDevice.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))) {
  85.             HandStartPos = LDevice.transform.pos;
  86.             arrow.SetActive (false);
  87.             return;
  88.         }
  89.         if (RDevice != null && (RDevice.GetPress(Valve.VR.EVRButtonId.k_EButton_Grip) || RDevice.GetPress(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))) {
  90.             Dragging ();
  91.             return;
  92.         }
  93.         if (LDevice != null && (LDevice.GetPress(Valve.VR.EVRButtonId.k_EButton_Grip) || LDevice.GetPress(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))) {
  94.             Dragging ();
  95.             return;
  96.         }
  97.         if (RDevice != null && (RDevice.GetPressUp(Valve.VR.EVRButtonId.k_EButton_Grip) || RDevice.GetPressUp(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))) {
  98.             Release ();
  99.         }
  100.         if (LDevice != null && (LDevice.GetPressUp(Valve.VR.EVRButtonId.k_EButton_Grip) || LDevice.GetPressUp(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))) {
  101.             Release ();
  102.         }
  103.     }
  104.  
  105.     protected override void Release()
  106.     {
  107.         print (Vector3.Distance (HandStartPos, CurrentHandPos));
  108.         if (Vector3.Distance (HandStartPos, CurrentHandPos) > 0.1f) {
  109.            
  110.             Vector3 rotationForce;
  111.             if (LDevice != null) {
  112.                 rotationForce = originalMousePosition - (LHand.transform.position * 50 * (1 + (Vector3.Distance (CurrentHandPos, HandStartPos) * 2)));
  113.             } else if (RDevice != null) {
  114.                 rotationForce = originalMousePosition - (RHand.transform.position * 50 * (1 + (Vector3.Distance (CurrentHandPos, HandStartPos) * 2)));
  115.             } else {
  116.                 rotationForce = originalMousePosition - Input.mousePosition;
  117.             }
  118.             float y = rotationForce.y;
  119.             //print(y+" is the reason y");
  120.             if (y > 0) {
  121.                 spinCount++;
  122.                 hasSpun = true;
  123.                 GetComponent<Rigidbody> ().AddForceAtPosition (rotationForce * forceAmount, originalCollisionPosition);
  124.             } else {
  125.                 spinCount++;
  126.                 hasSpun = true;
  127.                 GetComponent<Rigidbody> ().AddForceAtPosition (rotationForce * forceAmount, originalCollisionPosition);
  128.             }
  129.             lastPoint = Vector3.zero;
  130.         }
  131.     }
  132.  
  133.     /// <summary>
  134.     /// Makes the object rotate with the mouse movement.
  135.     /// </summary>
  136.     /// <param name="point"></param>
  137.     private void LookAtThis(Vector3 point)
  138.     {
  139.         point.x = 0;
  140.         point.y -= transform.position.y;
  141.         point.z -= transform.position.z;
  142.  
  143.         //if (firstClick)
  144.         {
  145.             rotationOffset = point;
  146.             //originalMousePosition = Input.mousePosition;
  147.             if(LDevice!=null){originalMousePosition = LHand.transform.position;}
  148.             if(RDevice!=null){originalMousePosition = RHand.transform.position;}
  149.             firstClick = false;
  150.         }
  151.         Quaternion blarg = Quaternion.FromToRotation(rotationOffset, point);
  152.         transform.rotation = Quaternion.Slerp(transform.rotation, blarg, t);
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement