using UnityEngine;
public class WiperController : MonoBehaviour
{
public float[] wiperSpeeds = { 0, 30, 60, 90 }; // 四个挡位的速度
public float maxAngle = 90f; // 最大角度
public Transform wiperArmTransform; // 雨刮器臂的Transform
public float defaultSpeed = 20f; // 默认恢复速度
private float currentAngle = 0f; // 当前雨刮器臂的角度
private int currentSpeedIndex = 0; // 当前的挡位
private float targetAngle = 0f; // 目标角度
private bool isWiping = false; // 是否在雨刮
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha0))
{
// 切换到挡位0,开始慢慢恢复到0度
currentSpeedIndex = 0;
targetAngle = 0f;
isWiping = false;
}
else if (Input.GetKeyDown(KeyCode.Alpha1))
{
// 切换到挡位1
currentSpeedIndex = 1;
isWiping = true;
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
// 切换到挡位2
currentSpeedIndex = 2;
isWiping = true;
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
// 切换到挡位3
currentSpeedIndex = 3;
isWiping = true;
}
// 更新雨刮器角度
if (isWiping)
{
float speed = wiperSpeeds[currentSpeedIndex];
float targetAngleDelta = speed * Time.deltaTime;
float angleDelta = Mathf.Clamp(targetAngle - currentAngle, -targetAngleDelta, targetAngleDelta);
currentAngle += angleDelta;
if (Mathf.Abs(currentAngle - targetAngle) < 0.1f)
{
targetAngle = (targetAngle == 0f) ? maxAngle : 0f;
}
wiperArmTransform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, currentAngle));
}
else if (Mathf.Abs(currentAngle) > 0.1f)
{
// 如果正在恢复到0度,则按默认速度恢复
float delta = Mathf.Sign(-currentAngle) * defaultSpeed * Time.deltaTime;
currentAngle += delta;
if (Mathf.Abs(currentAngle) < Mathf.Abs(delta))
{
currentAngle = 0f;
}
wiperArmTransform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, currentAngle));
}
}
}
只是简单的从0到90度反复刷,没有写特别复杂的功能