initial commit
This commit is contained in:
63
Assets/Scripts/Lesson_3/StrikerLeverController.cs
Normal file
63
Assets/Scripts/Lesson_3/StrikerLeverController.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user