190 lines
5.1 KiB
C#
190 lines
5.1 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 GameObject childAnimator; // 直接拖动绑定
|
|
|
|
public void SetTriggerParameter(string parameterName)
|
|
{
|
|
if (childAnimator != null)
|
|
{
|
|
Destroy(childAnimator);
|
|
}
|
|
}
|
|
|
|
}
|