using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Canvas_Obj : MonoBehaviour
{
private const float MAX_SPEED_ANGLE = -166; // 儀錶板角度 (開始刻度)
private const float ZERO_SPEED_ANGLE = 180; // 儀錶板角度 (結束刻度)
public RectTransform needleTranform; // 儀錶板指針
private float speedMax;
private float speed;
public float forward_Speed = 50; // 加速儀錶板指針
public float break_Speed = -100; // 減速加入儀表板指針
public float null_Speed = -20; // 未加速儀表板指針
void Awake()
{
speed = 0;
speedMax = 220f;
}
void Update()
{
HadlePlayerInput();
needleTranform.eulerAngles = new Vector3(0, 0, GetSpeedRotation());
}
private void HadlePlayerInput()
{
float vertical = Input.GetAxis("Vertical");
float pointerSpeed = (vertical > 0) ? pointerSpeed = forward_Speed : (vertical < 0) ? pointerSpeed = break_Speed : pointerSpeed = null_Speed;
speed += pointerSpeed * Time.deltaTime;
speed = Mathf.Clamp(speed, 0f, speedMax);
}
private float GetSpeedRotation()
{
float totalAngleSize = ZERO_SPEED_ANGLE - MAX_SPEED_ANGLE;
float speedNormalized = speed / speedMax;
return ZERO_SPEED_ANGLE - speedNormalized * totalAngleSize;
}
}
結果圖: