Files
PrinceOfGlory/Assets/Scripts/HandCatch/GrabItem.cs
kridoo 5ba318796f 1
2025-11-14 19:34:36 +08:00

206 lines
5.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrabItem : MonoBehaviour
{
public bool restIsBack = false; //放开后是否返回原位置
public bool isUseRigidbody = false; //是否使用重力
private Vector3 startPosition = Vector3.zero; //初始位置
public bool startIsUseRigid = true;
private Rigidbody m_rig;
public Transform m_originParent;
private CatchManager m_catch;
public AudioSource cathcAudio;
public AudioSource downAudio;
private string nowCatchHandName = "";
public EffectHideAndShowCtr effectHideAndShowCtr;
//计时相关
bool isRun = false;
float waitTime = 2f;
float runTime = 0;
//计时等待显示出现的动画(特效)
bool isRun2 = false;
float waitTime2 = 1f;
float runTime2 = 0;
private void Awake()
{
m_rig = GetComponent<Rigidbody>();
if(effectHideAndShowCtr == null)
effectHideAndShowCtr = GetComponent<EffectHideAndShowCtr>();
m_originParent = transform.parent;
startPosition = this.transform.localPosition;
SetRigState(startIsUseRigid);
}
private void Update()
{
if (isRun)
{
runTime += Time.deltaTime;
if (runTime >= waitTime)
{
//开始隐藏
isRun = false;
runTime = 0;
HideObj();
}
}
}
void HideObj()
{
if (effectHideAndShowCtr != null)
{
//this.GetComponent<BoxCollider>().enabled = false;
effectHideAndShowCtr.Hide();
StartCoroutine(ShowObj());
}
}
IEnumerator ShowObj()
{
yield return new WaitForSeconds(1);
this.GetComponent<BoxCollider>().isTrigger = true;
//transform.SetParent(m_originParent);
SetRigState(false);
isUseRigidbody = false;
SetStartPosition();
effectHideAndShowCtr.Show();
SetMoveMent(true);
//StartCoroutine(SetEnabled());
}
//IEnumerator SetEnabled()
//{
// yield return new WaitForSeconds(1);
// this.GetComponent<BoxCollider>().enabled = true;
//}
public void OnReset(string name)
{
if (nowCatchHandName != name) return;
if (downAudio != null)
downAudio.Play();
if (restIsBack)
{
isRun = true;
this.GetComponent<BoxCollider>().isTrigger = false;
isUseRigidbody = true;
transform.SetParent(m_originParent);
SetRigState(true);
}
else
{
transform.SetParent(m_originParent);
SetRigState(true);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("RightHand"))
{
//Debug.Log("Ent碰到了:"+other.gameObject.name);
//if (m_catch == null)
m_catch = other.GetComponent<CatchManager>();
SetTriggerParameter("DisTrigger");
if (m_catch == null) return;
// Debug.Log("111是否开始:"+ m_catch.Start);
if (m_catch.Start)
{
isRun = false;
runTime = 0;
nowCatchHandName = other.gameObject.name;
SetMoveMent(false);
SetRigState(false);
m_catch.SetParent(transform);
m_catch.SetCurrentItem(this);
if (cathcAudio != null)
cathcAudio.Play();
}
}
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("RightHand"))
{
//Debug.Log("Stay碰到了"+ other.gameObject.name);
//if (m_catch == null)
m_catch = other.GetComponent<CatchManager>();
if (m_catch == null) return;
//Debug.Log("222是否开始:" + m_catch.Start);
if (m_catch.Start)
{
isRun = false;
runTime = 0;
nowCatchHandName = other.gameObject.name;
SetMoveMent(false);
SetRigState(false);
m_catch.SetParent(transform);
m_catch.SetCurrentItem(this);
}
}
}
private void SetRigState(bool value)
{
if (isUseRigidbody)
{
m_rig.useGravity = value;
m_rig.isKinematic = !m_rig.useGravity;
}
}
private void SetStartPosition()
{
this.transform.localRotation = new Quaternion(0,0,0,0);
this.transform.localPosition = startPosition;
}
void SetMoveMent(bool isMove)
{
if (this.GetComponent<UpDownMovement>() != null)
{
Debug.Log("设置移动:"+isMove);
this.GetComponent<UpDownMovement>().isMove = isMove;
}
}
[Header("动画控制")]
[SerializeField] private Animator childAnimator; // 直接拖动绑定
public void SetTriggerParameter(string parameterName)
{
if (childAnimator != null)
{
childAnimator.SetTrigger(parameterName);
}
}
public void SetIntegerParameter(string parameterName, int value)
{
if (childAnimator != null)
{
childAnimator.SetInteger(parameterName, value);
}
}
// 示例方法:播放特定动画状态
public void PlayAnimationState(string stateName)
{
if (childAnimator != null)
{
childAnimator.Play(stateName);
}
}
}