87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class CraneController : MonoBehaviour
|
||
{
|
||
[Header("物体")]
|
||
public Transform bridge; // 横杆
|
||
public Transform hook; // 滑轮
|
||
public SkinnedMeshRenderer hookRenderer;//吊钩
|
||
|
||
[Header("移动速度")]
|
||
[Range(1, 10)]
|
||
public float bridgeSpeed = 5f; // 横杆移动速度
|
||
[Range(1, 10)]
|
||
public float hookSpeed = 5f; // 滑轮移动速度
|
||
|
||
public float blendShapeSpeed = 30f;
|
||
|
||
[Header("移动范围限制 (Z轴)")]
|
||
[Range(-1, -20)]
|
||
public float bridgeMinZ = -5f;
|
||
[Range(1, 20)]
|
||
public float bridgeMaxZ = 5f;
|
||
[Range(-1,5)]
|
||
public float hookMinZ = -3f;
|
||
[Range(1, 5)]
|
||
public float hookMaxZ = 3f;
|
||
|
||
[Header("BlendShape设置")]
|
||
public int hookBlendShapeIndex = 0; // BlendShape索引(通常是0)
|
||
public float minBlendShapeValue = 0f; // 吊钩收起
|
||
public float maxBlendShapeValue = 100f; // 吊钩放下
|
||
|
||
private float currentBlendValue = 0f;
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
HandleBridgeMovement();
|
||
HandleHookMovement();
|
||
HandleHookLift();
|
||
}
|
||
|
||
void HandleBridgeMovement()
|
||
{
|
||
// I/K 控制横杆前后
|
||
float input = 0;
|
||
if (Input.GetKey(KeyCode.I))
|
||
input = 1;
|
||
else if (Input.GetKey(KeyCode.K))
|
||
input = -1;
|
||
Vector3 newPos = bridge.position + Vector3.forward * input * bridgeSpeed * Time.deltaTime;
|
||
newPos.z = Mathf.Clamp(newPos.z, bridgeMinZ, bridgeMaxZ);
|
||
bridge.position = newPos;
|
||
}
|
||
|
||
void HandleHookMovement()
|
||
{
|
||
// J/L 控制滑轮前后
|
||
float input = 0;
|
||
if (Input.GetKey(KeyCode.J))
|
||
input = 1;
|
||
else if (Input.GetKey(KeyCode.L))
|
||
input = -1;
|
||
Vector3 newPos = hook.position + Vector3.left * input * hookSpeed * Time.deltaTime;
|
||
newPos.x = Mathf.Clamp(newPos.x, hookMinZ, hookMaxZ);
|
||
hook.position = newPos;
|
||
}
|
||
|
||
void HandleHookLift()
|
||
{
|
||
if (hookRenderer == null)
|
||
return;
|
||
|
||
float input = 0;
|
||
if (Input.GetKey(KeyCode.U))
|
||
input = 1; // 上升
|
||
else if (Input.GetKey(KeyCode.O))
|
||
input = -1; // 下降
|
||
|
||
currentBlendValue += input * blendShapeSpeed * Time.deltaTime;
|
||
currentBlendValue = Mathf.Clamp(currentBlendValue, minBlendShapeValue, maxBlendShapeValue);
|
||
|
||
hookRenderer.SetBlendShapeWeight(hookBlendShapeIndex, currentBlendValue);
|
||
}
|
||
}
|