仪表盘
using System;
using UnityEngine;
using UnityEngine.UI;

public class TestController : MonoBehaviour
{
	public Image imageNeedle;
	public Slider slider;

	private float currentSpeed = 0;
	private float targetSpeed = 0;
	private float neddleSpeed = 100;

	private void Update()
	{
		if (targetSpeed != currentSpeed)
		{
			UpdateSpeed();
		}
	}

	public void SetSpeedFromSlider()
	{
		targetSpeed = slider.value;
	}

	public void SetSpeed(float amt)
	{
		targetSpeed = amt;
	}

	public void UpdateSpeed()
	{
		if (targetSpeed > currentSpeed)
		{
			currentSpeed += Time.deltaTime * neddleSpeed;
			currentSpeed = Mathf.Clamp(currentSpeed, 0, targetSpeed);
		}
		else if (targetSpeed < currentSpeed)
		{
			currentSpeed -= Time.deltaTime * neddleSpeed;
			currentSpeed = Mathf.Clamp(currentSpeed, targetSpeed, 120);
		}

		SetNeedle();
	}

	public void SetNeedle()
	{
		print(Mathf.Lerp(210, -30, currentSpeed / 120));
		// imageNeedle.transform.localEulerAngles = new Vector3(0, 0, (currentSpeed / 120 * 240 - 120) * -1.0f);
		imageNeedle.transform.localEulerAngles = new Vector3(0, 0, Mathf.Lerp(210, -30, currentSpeed / 120));
	}
}
上一篇
下一篇