64 lines
1.7 KiB
C#
64 lines
1.7 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class StrikerLeverController : MonoBehaviour
|
|
{
|
|
public float endAngle = 8f;
|
|
public float currentAngle;
|
|
public Transform leverObject;
|
|
|
|
public Transform[] springsObjects;
|
|
public float springMaxScaleZ = 3.3f;
|
|
|
|
public bool isLength = false;
|
|
public TextMeshPro valueText;
|
|
|
|
public float springValue = 0f;
|
|
public float maxSpringValue = 1000f;
|
|
|
|
public bool isBlocked = false;
|
|
|
|
private void Awake()
|
|
{
|
|
currentAngle = endAngle;
|
|
}
|
|
void Update()
|
|
{
|
|
if (isLength)
|
|
valueText.text = $"{springValue:F2} CM".Replace(",", ".");
|
|
else
|
|
valueText.text = $"{springValue:F2} N/m".Replace(",", ".");
|
|
|
|
if (isBlocked) SetAlpha(0.2f);
|
|
}
|
|
|
|
|
|
public void RotateLever(float angle)
|
|
{
|
|
if (isBlocked) return;
|
|
|
|
currentAngle += angle;
|
|
if (currentAngle < endAngle) currentAngle = endAngle;
|
|
if (currentAngle > 180f - endAngle) currentAngle = 180f - endAngle;
|
|
|
|
foreach (Transform t in springsObjects) {
|
|
Vector3 newScale = t.localScale;
|
|
newScale.z = Remap(currentAngle, endAngle, 180f - endAngle, 0f, springMaxScaleZ);
|
|
t.localScale = newScale;
|
|
}
|
|
springValue = Remap(currentAngle, endAngle, 180f - endAngle, 0, maxSpringValue);
|
|
leverObject.localRotation = Quaternion.Euler(0f, 0f, currentAngle);
|
|
}
|
|
|
|
private float Remap(float v, float inMin, float inMax, float outMin, float outMax)
|
|
{
|
|
return (v - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
|
|
}
|
|
|
|
private void SetAlpha(float alpha)
|
|
{
|
|
leverObject.GetChild(0).GetComponent<Renderer>().material.color = new UnityEngine.Color(1, 1, 1, alpha);
|
|
}
|
|
|
|
}
|