using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BNG { /// /// Helper class to interact with physical sliders /// public class Slider : MonoBehaviour { /// /// How far away slider has moved from inital RangeLow position /// public float SlidePercentage { get { return _slidePercentage; } } private float _slidePercentage; /// /// Event to call if slider's value changes /// public FloatEvent onSliderChange; float lastSliderPercentage; float slideRangeLow = -0.15f; float slideRangeHigh = 0.15f; float slideRange; void Start() { ConfigurableJoint cj = GetComponent(); if (cj) { slideRangeLow = cj.linearLimit.limit * -1; slideRangeHigh = cj.linearLimit.limit; } slideRange = slideRangeHigh - slideRangeLow; } void Update() { _slidePercentage = ( (transform.localPosition.x - 0.001f) + slideRangeHigh) / slideRange; _slidePercentage = Mathf.Ceil(_slidePercentage * 100); if (_slidePercentage != lastSliderPercentage) { OnSliderChange(_slidePercentage); } lastSliderPercentage = _slidePercentage; } // Callback for lever percentage change public virtual void OnSliderChange(float percentage) { if(onSliderChange != null) { onSliderChange.Invoke(percentage); } } } }