111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
using UnityEngine;
|
||
|
||
public class CraneController : MonoBehaviour
|
||
{
|
||
public static CraneController Instance;
|
||
[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;
|
||
|
||
private float baseColliderZ;
|
||
|
||
private float Max = 0f;
|
||
private float Min = 0f;
|
||
|
||
public BoxCollider hookCollider; //钩子的碰撞
|
||
|
||
private void Awake()
|
||
{
|
||
Instance = this;
|
||
}
|
||
public void Start()
|
||
{
|
||
Max = -(2.49513f - 0.5113189f) / 100;
|
||
Min = (6.295807f - 0.3099952f) / 100;
|
||
}
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
HandleBridgeMovement();
|
||
HandleHookMovement();
|
||
HandleHookLift();
|
||
hookSize();
|
||
}
|
||
|
||
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);
|
||
}
|
||
public void hookSize()
|
||
{
|
||
if (hookCollider != null)
|
||
{
|
||
hookCollider.center = new Vector3(-0.1262663f, -0.01883008f, -0.5113189f + currentBlendValue * Max);
|
||
hookCollider.size = new Vector3(0.1096101f, 0.2627291f, 0.3099952f + currentBlendValue * Min);
|
||
}
|
||
}
|
||
}
|