247 lines
6.1 KiB
C#
247 lines
6.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class MenShiManager : MonoBehaviour
|
||
{
|
||
public static MenShiManager Instance;
|
||
|
||
private void Awake()
|
||
{
|
||
Instance = this;
|
||
}
|
||
|
||
[Header("物体")]
|
||
public Transform bridge; // 起重机
|
||
public Transform jia; // 驾驶室
|
||
public SkinnedMeshRenderer hookRenderer;//吊钩
|
||
|
||
public Transform[] YaoGan; //3个摇杆
|
||
|
||
[Header("移动速度")]
|
||
[Range(1, 10)]
|
||
public float bridgeSpeed = 0.1f; // 起重机移动速度
|
||
[Range(1, 10)]
|
||
public float hookSpeed = 0.1f; // 驾驶室移动速度
|
||
|
||
public float blendShapeSpeed = 30f;
|
||
|
||
[Header("移动范围限制 (X轴)")]
|
||
[Range(-0.35f, 0.25f)]
|
||
public float bridgeMinX = -0.35f;
|
||
[Range(-0.35f, 0.25f)]
|
||
public float bridgeMaxX = 0.25f;
|
||
[Range(-0.19f, 0.04f)]
|
||
public float jiaMinY = -0.19f;
|
||
[Range(-0.19f, 0.04f)]
|
||
public float jiaMaxY = 0.04f;
|
||
|
||
[Header("BlendShape设置")]
|
||
public int hookBlendShapeIndex = 0; // BlendShape索引(通常是0)
|
||
public float minBlendShapeValue = 0f; // 吊钩收起
|
||
public float maxBlendShapeValue = 100f; // 吊钩放下
|
||
|
||
public float currentBlendValue = 0f;
|
||
|
||
private float baseColliderZ;
|
||
|
||
private float Max = 0f;
|
||
private float Min = 0f;
|
||
|
||
public BoxCollider hookCollider; //钩子的碰撞
|
||
|
||
// 按钮控制标志
|
||
private bool isBridgeLeft = false;
|
||
private bool isBridgeRight = false;
|
||
private bool isHookForward = false; // 对应 J 键 (input=1, 向 -x 移动)
|
||
private bool isHookBackward = false; // 对应 L 键 (input=-1, 向 +x 移动)
|
||
private bool isHookUp = false; // 对应 U 键 (上升)
|
||
private bool isHookDown = false; // 对应 O 键 (下降)
|
||
|
||
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();
|
||
}
|
||
|
||
|
||
void HandleBridgeMovement()
|
||
{
|
||
// I/K 或按钮控制横杆前后
|
||
float input = 0f;
|
||
|
||
// 键盘输入
|
||
if (Input.GetKey(KeyCode.I))
|
||
input += 1f;
|
||
if (Input.GetKey(KeyCode.K))
|
||
input -= 1f;
|
||
|
||
// 按钮输入
|
||
if (isBridgeLeft)
|
||
input += 1f;
|
||
if (isBridgeRight)
|
||
input -= 1f;
|
||
|
||
// 限制输入范围,避免超速
|
||
input = Mathf.Clamp(input, -1f, 1f);
|
||
|
||
Vector3 newbrigePos = bridge.localPosition + Vector3.right * input * bridgeSpeed * Time.deltaTime;
|
||
newbrigePos.x = Mathf.Clamp(newbrigePos.x, bridgeMinX, bridgeMaxX);
|
||
bridge.localPosition = newbrigePos;
|
||
|
||
|
||
}
|
||
|
||
void HandleHookMovement()
|
||
{
|
||
// J/L 或按钮控制滑轮前后
|
||
float input = 0f;
|
||
|
||
// 键盘输入
|
||
if (Input.GetKey(KeyCode.J))
|
||
input += 1f; // input=1,向 -x 移动
|
||
if (Input.GetKey(KeyCode.L))
|
||
input -= 1f; // input=-1,向 +x 移动
|
||
|
||
// 按钮输入
|
||
if (isHookForward)
|
||
input += 1f;
|
||
if (isHookBackward)
|
||
input -= 1f;
|
||
|
||
// 限制输入范围,避免超速
|
||
input = Mathf.Clamp(input, -1f, 1f);
|
||
|
||
Vector3 newjiaPos = jia.localPosition + Vector3.up * input * hookSpeed * Time.deltaTime;
|
||
newjiaPos.y = Mathf.Clamp(newjiaPos.y, jiaMinY, jiaMaxY);
|
||
jia.localPosition = newjiaPos;
|
||
}
|
||
|
||
void HandleHookLift()
|
||
{
|
||
if (hookRenderer == null)
|
||
return;
|
||
|
||
float input = 0f;
|
||
|
||
// 键盘输入
|
||
if (Input.GetKey(KeyCode.U))
|
||
input += 1f; // 上升
|
||
if (Input.GetKey(KeyCode.O))
|
||
input -= 1f; // 下降
|
||
|
||
// 按钮输入
|
||
if (isHookUp)
|
||
input -= 1f;
|
||
if (isHookDown)
|
||
input += 1f;
|
||
|
||
// 限制输入范围,避免超速
|
||
input = Mathf.Clamp(input, -1f, 1f);
|
||
|
||
currentBlendValue += input * blendShapeSpeed * Time.deltaTime;
|
||
currentBlendValue = Mathf.Clamp(currentBlendValue, minBlendShapeValue, maxBlendShapeValue);
|
||
|
||
hookRenderer.SetBlendShapeWeight(hookBlendShapeIndex, currentBlendValue);
|
||
}
|
||
|
||
//public void SethookColliderPos(bool isDi)
|
||
//{
|
||
// if (isDi)
|
||
// {
|
||
// hookCollider.center = new Vector3(hookCollider.center.x, hookCollider.center.y, -0.15f);
|
||
// }
|
||
// else
|
||
// {
|
||
|
||
// }
|
||
//}
|
||
|
||
// ========== 按钮控制公共方法 ==========
|
||
// 桥前进 (对应 I 键)
|
||
public void StartBridgeLeft()
|
||
{
|
||
isBridgeLeft = true;
|
||
YaoGan[0].rotation = Quaternion.Euler(0f, 0f, -25f);
|
||
}
|
||
|
||
public void StopBridgeLeft()
|
||
{
|
||
isBridgeLeft = false;
|
||
YaoGan[0].rotation = Quaternion.Euler(0f, 0f, 0f);
|
||
}
|
||
|
||
// 桥后退 (对应 K 键)
|
||
public void StartBridgeRight()
|
||
{
|
||
isBridgeRight = true;
|
||
YaoGan[0].rotation = Quaternion.Euler(0f, 0f, 25f);
|
||
}
|
||
|
||
public void StopBridgeRight()
|
||
{
|
||
isBridgeRight = false;
|
||
YaoGan[0].rotation = Quaternion.Euler(0f, 0f, 0f);
|
||
}
|
||
|
||
// 钩子左移 (对应 J 键,向 -x)
|
||
public void StartHookForward()
|
||
{
|
||
isHookForward = true;
|
||
YaoGan[0].rotation = Quaternion.Euler(-25f, 0f, 0f);
|
||
}
|
||
|
||
public void StopHookForward()
|
||
{
|
||
isHookForward = false;
|
||
YaoGan[0].rotation = Quaternion.Euler(0f, 0f, 0f);
|
||
}
|
||
|
||
// 钩子右移 (对应 L 键,向 +x)
|
||
public void StartHookBackward()
|
||
{
|
||
isHookBackward = true;
|
||
YaoGan[0].rotation = Quaternion.Euler(25f, 0f, 0f);
|
||
}
|
||
|
||
public void StopHookBackward()
|
||
{
|
||
isHookBackward = false;
|
||
YaoGan[0].rotation = Quaternion.Euler(0f, 0f, 0f);
|
||
}
|
||
|
||
// 钩子上? (对应 U 键)
|
||
public void StartHookUp()
|
||
{
|
||
isHookUp = true;
|
||
//YaoGan[2].rotation = Quaternion.Euler(-25f, 0f, 0f);
|
||
}
|
||
|
||
public void StopHookUp()
|
||
{
|
||
isHookUp = false;
|
||
//YaoGan[2].rotation = Quaternion.Euler(0f, 0f, 0f);
|
||
}
|
||
|
||
// 钩子下降 (对应 O 键)
|
||
public void StartHookDown()
|
||
{
|
||
isHookDown = true;
|
||
//YaoGan[2].rotation = Quaternion.Euler(25f, 0f, 0f);
|
||
}
|
||
|
||
public void StopHookDown()
|
||
{
|
||
isHookDown = false;
|
||
//YaoGan[2].rotation = Quaternion.Euler(0f, 0f, 0f);
|
||
}
|
||
}
|