ZhangZhouSpecialEquipment/Assets/Scripts/叉车/ForkliftController.cs

41 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 叉车
/// </summary>
public class ForkliftController : MonoBehaviour
{
[Header("Fork Settings")]
public GameObject _forks; // 前叉GameObject引用
[Header("最低高度")]
public float minHeight = 0f; // 最低高度
[Header("最高高度")]
public float maxHeight = 5f; // 最高高度
[Header("升降速度")]
public float liftSpeed = 2f; // 升降速度
void Update()
{
if (_forks == null) return;
//按下键盘上的上、下
float inputValue = 0f;
if (Input.GetKey(KeyCode.UpArrow)) // 升起
{
inputValue = 1f;
}
else if (Input.GetKey(KeyCode.DownArrow)) // 降下
{
inputValue = -1f;
}
// 移动逻辑沿本地up方向平移并clamp Y位置
Vector3 currentPos = _forks.transform.localPosition;
currentPos += transform.up * inputValue * liftSpeed * Time.deltaTime;
currentPos.y = Mathf.Clamp(currentPos.y, minHeight, maxHeight);
_forks.transform.localPosition = currentPos;
}
}