Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- public class HandleBarController : MonoBehaviour{
- [Tooltip("This is the actual object that the player will grab when grabbing the handlebars.")]
- [Required, SceneObjectsOnly]
- public SnapGrabbable leftBarGrabbable;
- [Tooltip("This is the actual object that the player will grab when grabbing the handlebars.")]
- [Required, SceneObjectsOnly]
- public SnapGrabbable rightBarGrabbable;
- [Tooltip("A transform marking the position directly in between the two grabbables.")]
- [Required, SceneObjectsOnly]
- public Transform grabbableNeutralPos;
- [Space(10)]
- [Tooltip("This is the point on the handlebars where the grabbables were snap to when not held.")]
- [Required, SceneObjectsOnly]
- public Transform leftGrabbableTrackPoint;
- [Tooltip("This is the point on the handlebars where the grabbables were snap to when not held.")]
- [Required, SceneObjectsOnly]
- public Transform rightGrabbableTrackPoint;
- [Space(10)]
- [Tooltip("A transform marking the pivot position of the bar.")]
- [Required, SceneObjectsOnly]
- public Transform barPivot;
- [Tooltip("The transform to be rotated as the root of the bar.")]
- [Required, SceneObjectsOnly]
- public Transform barRoot;
- [Tooltip("The visual object representing the bar, this might be different from the actual direction.")]
- [Required, SceneObjectsOnly]
- public Transform barVisual;
- [Space(10)]
- [Tooltip("The percentage of the true rotation that's applied to the visual.")]
- public float visualScaling = 1f;
- [Tooltip("The maximum turnable angle deviation from 0 (so 90 offers 180 degrees, from -90 to 90).")]
- [MinValue(-90), MaxValue(90)]
- public float maxAngle = 90f;
- [Header("Tutorial Switches")]
- public bool enableTurn = true;
- // We remember the original positions so we can return to them when ungrabbed
- private Vector3 leftBarOriginalLocalPos;
- private Vector3 rightBarOriginalLocalPos;
- void Start(){
- leftBarOriginalLocalPos = leftBarGrabbable.transform.localPosition;
- rightBarOriginalLocalPos = rightBarGrabbable.transform.localPosition;
- }
- void Update(){
- if(enableTurn){
- // Update the bar rotation
- // If the bar isn't held at all, we don't want to rotate it at all
- if(leftBarGrabbable.grabbedBy != null || rightBarGrabbable.grabbedBy != null){
- Quaternion barRot = ComputeBarRot();
- // The barRoot is our directional output, so it goes to the true rotation
- barRoot.localRotation = barRot;
- // The barVisual is just graphics, so we adjust it to be a percentage of the true rotation.
- barVisual.localRotation = Quaternion.LerpUnclamped(Quaternion.identity, barRot, visualScaling);
- }
- }
- // Move the grabbables to the proper position if not currently grabbed
- if(leftBarGrabbable.grabbedBy == null){
- leftBarGrabbable.transform.position = leftGrabbableTrackPoint.position;
- }
- if(rightBarGrabbable.grabbedBy == null){
- rightBarGrabbable.transform.position = rightGrabbableTrackPoint.position;
- }
- }
- private Quaternion ComputeBarRot(){
- // If the bar isn't held at all, we can't do further operations, so just return no rotation
- // It's the caller's responsibility to decide what to do if neither handlebar is grabbed and the identity rotation isn't desireable
- if(leftBarGrabbable.grabbedBy == null && rightBarGrabbable.grabbedBy == null){
- return Quaternion.identity;
- }
- // If the bar isn't held by one grabbable, then treat it as if it's in the direct center
- Vector3 leftPosition = leftBarGrabbable.grabbedBy != null ? leftBarGrabbable.transform.localPosition : grabbableNeutralPos.localPosition;
- Vector3 rightPosition = rightBarGrabbable.grabbedBy != null ? rightBarGrabbable.transform.localPosition : grabbableNeutralPos.localPosition;
- // Compute the direction that's 90 degrees off of the bar facing
- Vector3 dir90 = (leftPosition - rightPosition).normalized;
- // Zero out the y component since we don't care about height
- dir90.y = 0f;
- // Turn this into a rotation
- Quaternion rot90 = Quaternion.LookRotation(dir90);
- // Rotate by 90 degrees
- Quaternion targetRot = rot90 * Quaternion.AngleAxis(90f, Vector3.up);
- // Don't allow the handlebars to turn past the maxAngle in either direction
- if(targetRot.eulerAngles.y > maxAngle && targetRot.eulerAngles.y < 180f){
- targetRot = Quaternion.AngleAxis(maxAngle, Vector3.up);
- }else if(targetRot.eulerAngles.y < 360f - maxAngle && targetRot.eulerAngles.y > 180f){
- targetRot = Quaternion.AngleAxis(360f - maxAngle, Vector3.up);
- }
- // Return the rotation
- return targetRot;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment