41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
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;
|
||
}
|
||
}
|