JoshButner

Henchman - HandleBarController.cs

Mar 10th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5.  
  6. public class HandleBarController : MonoBehaviour{
  7.     [Tooltip("This is the actual object that the player will grab when grabbing the handlebars.")]
  8.     [Required, SceneObjectsOnly]
  9.     public SnapGrabbable leftBarGrabbable;
  10.     [Tooltip("This is the actual object that the player will grab when grabbing the handlebars.")]
  11.     [Required, SceneObjectsOnly]
  12.     public SnapGrabbable rightBarGrabbable;
  13.     [Tooltip("A transform marking the position directly in between the two grabbables.")]
  14.     [Required, SceneObjectsOnly]
  15.     public Transform grabbableNeutralPos;
  16.  
  17.     [Space(10)]
  18.  
  19.     [Tooltip("This is the point on the handlebars where the grabbables were snap to when not held.")]
  20.     [Required, SceneObjectsOnly]
  21.     public Transform leftGrabbableTrackPoint;
  22.     [Tooltip("This is the point on the handlebars where the grabbables were snap to when not held.")]
  23.     [Required, SceneObjectsOnly]
  24.     public Transform rightGrabbableTrackPoint;
  25.  
  26.     [Space(10)]
  27.  
  28.     [Tooltip("A transform marking the pivot position of the bar.")]
  29.     [Required, SceneObjectsOnly]
  30.     public Transform barPivot;
  31.     [Tooltip("The transform to be rotated as the root of the bar.")]
  32.     [Required, SceneObjectsOnly]
  33.     public Transform barRoot;
  34.     [Tooltip("The visual object representing the bar, this might be different from the actual direction.")]
  35.     [Required, SceneObjectsOnly]
  36.     public Transform barVisual;
  37.  
  38.     [Space(10)]
  39.  
  40.     [Tooltip("The percentage of the true rotation that's applied to the visual.")]
  41.     public float visualScaling = 1f;
  42.  
  43.     [Tooltip("The maximum turnable angle deviation from 0 (so 90 offers 180 degrees, from -90 to 90).")]
  44.     [MinValue(-90), MaxValue(90)]
  45.     public float maxAngle = 90f;
  46.  
  47.     [Header("Tutorial Switches")]
  48.     public bool enableTurn = true;
  49.  
  50.     // We remember the original positions so we can return to them when ungrabbed
  51.     private Vector3 leftBarOriginalLocalPos;
  52.     private Vector3 rightBarOriginalLocalPos;
  53.  
  54.     void Start(){
  55.         leftBarOriginalLocalPos = leftBarGrabbable.transform.localPosition;
  56.         rightBarOriginalLocalPos = rightBarGrabbable.transform.localPosition;
  57.     }
  58.  
  59.     void Update(){
  60.         if(enableTurn){
  61.             // Update the bar rotation
  62.             // If the bar isn't held at all, we don't want to rotate it at all
  63.             if(leftBarGrabbable.grabbedBy != null || rightBarGrabbable.grabbedBy != null){
  64.                 Quaternion barRot = ComputeBarRot();
  65.                 // The barRoot is our directional output, so it goes to the true rotation
  66.                 barRoot.localRotation = barRot;
  67.                 // The barVisual is just graphics, so we adjust it to be a percentage of the true rotation.
  68.                 barVisual.localRotation = Quaternion.LerpUnclamped(Quaternion.identity, barRot, visualScaling);
  69.             }
  70.         }
  71.  
  72.         // Move the grabbables to the proper position if not currently grabbed
  73.         if(leftBarGrabbable.grabbedBy == null){
  74.             leftBarGrabbable.transform.position = leftGrabbableTrackPoint.position;
  75.         }
  76.         if(rightBarGrabbable.grabbedBy == null){
  77.             rightBarGrabbable.transform.position = rightGrabbableTrackPoint.position;
  78.         }
  79.     }
  80.  
  81.     private Quaternion ComputeBarRot(){
  82.         // If the bar isn't held at all, we can't do further operations, so just return no rotation
  83.         // It's the caller's responsibility to decide what to do if neither handlebar is grabbed and the identity rotation isn't desireable
  84.         if(leftBarGrabbable.grabbedBy == null && rightBarGrabbable.grabbedBy == null){
  85.             return Quaternion.identity;
  86.         }
  87.  
  88.         // If the bar isn't held by one grabbable, then treat it as if it's in the direct center
  89.         Vector3 leftPosition = leftBarGrabbable.grabbedBy != null ? leftBarGrabbable.transform.localPosition : grabbableNeutralPos.localPosition;
  90.         Vector3 rightPosition = rightBarGrabbable.grabbedBy != null ? rightBarGrabbable.transform.localPosition : grabbableNeutralPos.localPosition;
  91.  
  92.         // Compute the direction that's 90 degrees off of the bar facing
  93.         Vector3 dir90 = (leftPosition - rightPosition).normalized;
  94.         // Zero out the y component since we don't care about height
  95.         dir90.y = 0f;
  96.  
  97.         // Turn this into a rotation
  98.         Quaternion rot90 = Quaternion.LookRotation(dir90);
  99.         // Rotate by 90 degrees
  100.         Quaternion targetRot = rot90 * Quaternion.AngleAxis(90f, Vector3.up);
  101.  
  102.         // Don't allow the handlebars to turn past the maxAngle in either direction
  103.         if(targetRot.eulerAngles.y > maxAngle && targetRot.eulerAngles.y < 180f){
  104.             targetRot = Quaternion.AngleAxis(maxAngle, Vector3.up);
  105.         }else if(targetRot.eulerAngles.y < 360f - maxAngle && targetRot.eulerAngles.y > 180f){
  106.             targetRot = Quaternion.AngleAxis(360f - maxAngle, Vector3.up);
  107.         }
  108.         // Return the rotation
  109.         return targetRot;
  110.     }
  111.  
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment