Advertisement
mew_fi

VR pointer controller

Dec 4th, 2022
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.81 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR;
  5. using UnityEngine.XR.Interaction.Toolkit;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.InputSystem;
  8. using UnityEngine.UI;
  9.  
  10. public class CylinderController : MonoBehaviour {
  11.     AudioSource audioLaser;
  12.     [SerializeField] float blastPower = 100;
  13.     public float angleOffset = 0, angleMultiplier = 1;
  14.     public float AnglePower = 1.25f;
  15.     [SerializeField] float maxLineRotation = 25;
  16.     [SerializeField] float hapticPower = 1, hapticDuration = 0.01f;
  17.     [SerializeField] float smoothingPos = 50;
  18.     [SerializeField] InputActionProperty primaryButton, secondaryButton;
  19.     [SerializeField] InputActionProperty move2D;
  20.     [SerializeField] InputActionProperty thumbPressure;
  21.     [SerializeField] LayerMask raycastLayermask;
  22.     [SerializeField] Transform reticle;
  23.     [SerializeField] Transform drawingLine;
  24.     public Transform Line;
  25.     public float LineRotationX, LineRotationY;
  26.  
  27.     public float activationDuration = 0.5f;
  28.     ActionBasedController xrController;
  29.     Rigidbody selectedObject;
  30.     Vector3 usedDirection;
  31.     float usedRotation;
  32.     Vector3 previoushitpoint;
  33.     Transform tempDrawLine;
  34.     TrailRenderer tempTrail;
  35.  
  36.     Color lineColor, fireColor;
  37.     public Material LineMaterial, ReticleMaterial;
  38.     public float SphereCastRadius = 0.25f;
  39.     public float LineAlpha = 0.3f;
  40.  
  41.     public Transform Player;
  42.     [SerializeField] float movementSpeed, movementLerp, turningSpeed, turningLerp;
  43.  
  44.     float triggerValue;
  45.     bool readyToFire = false;
  46.     float maxrotspeed = 0;
  47.    
  48.  
  49.     void Start() {
  50.         xrController = GetComponent<ActionBasedController>();
  51.         LineMaterial.color = new Color(1, 0, 0, .5f);
  52.         Line.gameObject.SetActive(true);
  53.         audioLaser = GetComponent<AudioSource>();
  54.         reticle.localScale = new Vector3(SphereCastRadius * 2, SphereCastRadius * 2, SphereCastRadius * 2);
  55.     }
  56.  
  57.     void Update() {
  58.         triggerValue = xrController.activateActionValue.action.ReadValue<float>();
  59.         ResetLevel();
  60.         MoveLine(ChangeFollowTightness());
  61.        
  62.  
  63.         if (xrController.activateAction.action.triggered && !readyToFire && tempDrawLine == null) {
  64.             maxrotspeed = 0;
  65.             audioLaser.Play();
  66.             CancelInvoke("StopActivation");
  67.             readyToFire = true;
  68.             Invoke("LineIsActive", 0);
  69.             Invoke("StopActivation", activationDuration);
  70.         }
  71.  
  72.  
  73.         if (xrController.activateAction.action.ReadValue<float>() < 1) {
  74.             readyToFire = false;
  75.             //tempDrawLine = null;
  76.         }
  77.         BlastTarget();
  78.         MovePlayer();
  79.        
  80.         // Test drawing
  81.         //Drawing();
  82.     }
  83.  
  84.     void Drawing() {
  85.         if (triggerValue > 0.95f && tempDrawLine == null) {
  86.             tempDrawLine = Instantiate(drawingLine, reticle.position, reticle.rotation);
  87.             tempTrail = tempDrawLine.GetComponent<TrailRenderer>();
  88.         }
  89.         if (tempDrawLine != null) {
  90.             tempDrawLine.position = reticle.position;
  91.         }
  92.         if (triggerValue < 0.9f && tempDrawLine != null) {
  93.             tempDrawLine = null;
  94.         }
  95.         if (xrController.activateAction.action.ReadValue<float>() == 0) {
  96.             Invoke("StopActivation", 0);
  97.         }
  98.     }
  99.  
  100.     void DisableDrawing() {
  101.         tempDrawLine = null;
  102.     }
  103.  
  104.     void LineIsActive() {
  105.         LineMaterial.color = new Color(0, 0, 0, LineAlpha);
  106.         ReticleMaterial.color = new Color(0, 0, 0, LineAlpha);
  107.     }
  108.  
  109.     void StopActivation() {
  110.         readyToFire = false;
  111.         LineMaterial.color = new Color(1, 0, 0, LineAlpha);
  112.         ReticleMaterial.color = new Color(1, 0, 0, LineAlpha);
  113.     }
  114.  
  115.  
  116.     void MovePlayer() {
  117.         Vector2 moveVector = move2D.action.ReadValue<Vector2>();
  118.         if (Mathf.Abs(moveVector.y) > Mathf.Abs(moveVector.x)) {
  119.             moveVector.x = 0;
  120.         } else if (Mathf.Abs(moveVector.y) > Mathf.Abs(moveVector.x)) {
  121.             moveVector.y = 0;
  122.         }
  123.        
  124.         Vector3 movementDirection = Line.forward * moveVector.y + Vector3.Scale(Line.right, new Vector3(1, 0, 1)) * moveVector.x;
  125.         float brakehelper;
  126.         if (movementDirection.magnitude < usedDirection.magnitude) brakehelper = 2;
  127.         else brakehelper = 1;
  128.         usedDirection = Vector3.Lerp(usedDirection, movementDirection, movementLerp * brakehelper * Time.deltaTime);
  129.  
  130.         float turnslower;
  131.         if (Mathf.Abs(moveVector.x) < Mathf.Abs(usedRotation)) turnslower = 2;
  132.         else turnslower = 1;
  133.         usedRotation =  Mathf.Lerp(usedRotation, moveVector.x, turningLerp * turnslower * Time.deltaTime);
  134.         Player.position += usedDirection * movementSpeed * Time.deltaTime;
  135.         Player.RotateAround(Camera.main.transform.position, Vector3.up, usedRotation * turningSpeed * Time.deltaTime);
  136.  
  137.         //Player.Rotate(Vector3.up, usedRotation * turningSpeed * Time.deltaTime);
  138.     }
  139.  
  140.     void MoveLine(float smoothing) {
  141.         Line.localPosition = Vector3.Lerp(Line.localPosition, xrController.positionAction.action.ReadValue<Vector3>(), smoothingPos * Time.deltaTime);
  142.         Line.rotation = Quaternion.Lerp(Line.rotation, xrController.transform.rotation * Quaternion.Euler(LineRotationX, LineRotationY, 0), smoothing * Time.deltaTime);
  143.     }
  144.     float ChangeFollowTightness() {
  145.         // --------------------------------
  146.         // An attempt to approximate for wrist flick velocity
  147.         // when calculating throwing power
  148.         float testAngle = Vector3.Angle(Line.forward, xrController.transform.forward);
  149.         //float test = angleMultiplier * Mathf.Pow(testAngle, AnglePower) + angleOffset;
  150.         if (testAngle > maxrotspeed) maxrotspeed = testAngle;
  151.         //if (transform.name == "RightHand Controller") {
  152.         //    Debug.Log(maxrotspeed.ToString("f3"));
  153.         //}
  154.         // This is affected by anglemultiplier and angle power obviously, but a pretty good rough estimate nevertheless.
  155.         // --------------------------------
  156.        
  157.        
  158.         float smoothing;
  159.         float theAngle = Quaternion.Angle(Line.rotation, xrController.transform.rotation * Quaternion.Euler(LineRotationX, LineRotationY, 0));
  160.         smoothing = angleMultiplier * Mathf.Pow(theAngle, AnglePower) + angleOffset;
  161.         if (smoothing > maxLineRotation) smoothing = maxLineRotation;
  162.         return smoothing;
  163.     }
  164.  
  165.     void BlastTarget() {
  166.         RaycastHit rayhit;
  167.         if (Physics.Raycast(Line.position, Line.forward, out rayhit, Mathf.Infinity, raycastLayermask)) {
  168.             reticle.position = Line.position + Line.forward * (rayhit.distance - 0.01f);
  169.             if (rayhit.rigidbody != null && readyToFire && xrController.activateAction.action.triggered) {
  170.                 rayhit.rigidbody.AddForceAtPosition(Line.up * blastPower * triggerValue, rayhit.point, ForceMode.Impulse);
  171.                 xrController.SendHapticImpulse(hapticPower, hapticDuration);
  172.                 //audioLaser.Play();
  173.                 //Invoke("ResetLaserColor", 0.05f);
  174.                 Invoke("StopActivation", 0);
  175.             }
  176.  
  177.             if (rayhit.collider.gameObject.layer == 6 && readyToFire && xrController.activateAction.action.triggered) {
  178.                 rayhit.collider.gameObject.GetComponent<TargetBehaviorSpherical>().DecideNewPosition(0);
  179.                 xrController.SendHapticImpulse(hapticPower, hapticDuration);
  180.                 //audioLaser.Play();
  181.                 //Invoke("ResetLaserColor", 0.05f);
  182.                 Invoke("StopActivation", 0);
  183.                 readyToFire = false;
  184.             }
  185.  
  186.             if (rayhit.collider.gameObject.layer == 5) {
  187.                 rayhit.collider.gameObject.GetComponent<Button>().onClick.Invoke();
  188.             }
  189.  
  190.         } else {
  191.             reticle.position = Line.position + Line.forward * 100;
  192.         }
  193.         RaycastHit[] spherecasthits;
  194.         spherecasthits = Physics.SphereCastAll(Line.position, SphereCastRadius, Line.forward, Mathf.Infinity, raycastLayermask);
  195.         foreach (RaycastHit hit in spherecasthits) {
  196.             if (hit.rigidbody != null && readyToFire && xrController.activateAction.action.triggered) {
  197.                 hit.rigidbody.AddForceAtPosition(Line.up * blastPower * triggerValue, hit.point, ForceMode.Impulse);
  198.                 xrController.SendHapticImpulse(hapticPower, hapticDuration);
  199.                 //audioLaser.Play();
  200.                 //Invoke("ResetLaserColor", 0.05f);
  201.                 Invoke("StopActivation", 0);
  202.                 break;
  203.             }
  204.             if (hit.collider.gameObject.layer == 6 && readyToFire && xrController.activateAction.action.triggered) {
  205.                 hit.collider.gameObject.GetComponent<TargetBehaviorSpherical>().DecideNewPosition(1);
  206.                 xrController.SendHapticImpulse(hapticPower, hapticDuration);
  207.                 //audioLaser.Play();
  208.                 //Invoke("ResetLaserColor", 0.05f);
  209.                 Invoke("StopActivation", 0);
  210.                 readyToFire = false;
  211.                 break;
  212.             }
  213.         }
  214.  
  215.         RaycastHit[] previoushits;
  216.         previoushits = null;
  217.         previoushits = Physics.SphereCastAll(reticle.position, SphereCastRadius, previoushitpoint - reticle.position, (previoushitpoint - reticle.position).magnitude, raycastLayermask);      
  218.  
  219.         foreach (RaycastHit hit in previoushits) {
  220.             if (hit.rigidbody != null && readyToFire) {
  221.                 hit.rigidbody.AddForceAtPosition(Line.up * blastPower * triggerValue, hit.point, ForceMode.Impulse);
  222.                 xrController.SendHapticImpulse(hapticPower, hapticDuration);
  223.                 //audioLaser.Play();
  224.                 //Invoke("ResetLaserColor", 0.05f);
  225.                 Invoke("StopActivation", 0);
  226.                 break;
  227.             }
  228.             if (hit.collider.gameObject.layer == 6 && readyToFire) {
  229.                 hit.collider.gameObject.GetComponent<TargetBehaviorSpherical>().DecideNewPosition(2);
  230.                 xrController.SendHapticImpulse(hapticPower, hapticDuration);
  231.                 //audioLaser.Play();
  232.                 //Invoke("ResetLaserColor", 0.05f);
  233.                 Invoke("StopActivation", 0);
  234.                 readyToFire = false;
  235.                 break;
  236.             }
  237.         }
  238.         previoushitpoint = reticle.position;
  239.  
  240.     }
  241.     void ResetLaserColor() {
  242.         LineMaterial.color = new Color(1, 0, 0, LineAlpha);
  243.         reticle.GetComponent<Material>().color = new Color(1, 0, 0, LineAlpha);
  244.     }
  245.  
  246.     void ResetLevel() {
  247.         if (secondaryButton.action.triggered && transform.name == "RightHand Controller"){
  248.             SceneManager.LoadScene(1, LoadSceneMode.Single);
  249.         }
  250.         if (primaryButton.action.triggered && transform.name == "RightHand Controller") {
  251.             SceneManager.LoadScene(0, LoadSceneMode.Single);
  252.         }
  253.     }
  254.  
  255. }
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement