199 lines
5.5 KiB
C#
199 lines
5.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class GrabItem : MonoBehaviour
|
||
{
|
||
public bool restIsBack = false; //<2F>ſ<EFBFBD><C5BF><EFBFBD><EFBFBD>Ƿ<C7B7>ԭλ<D4AD><CEBB>
|
||
public bool isUseRigidbody = false; //<2F>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
private Vector3 startPosition = Vector3.zero; //<2F><>ʼλ<CABC><CEBB>
|
||
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;
|
||
|
||
//<2F><>ʱ<EFBFBD><CAB1><EFBFBD>
|
||
bool isRun = false;
|
||
float waitTime = 2f;
|
||
float runTime = 0;
|
||
|
||
//<2F><>ʱ<EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ֵĶ<D6B5><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>
|
||
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)
|
||
{
|
||
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
||
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<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"+other.gameObject.name);
|
||
SetTriggerParameter("DisTrigger");
|
||
SceneMgr.Instance.isTrigger=true;
|
||
//if (m_catch == null)
|
||
m_catch = other.GetComponent<CatchManager>();
|
||
if (m_catch == null) return;
|
||
// Debug.Log("111<31>Ƿ<EFBFBD>ʼ:"+ 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 OnTriggerExit(Collider other)
|
||
{
|
||
if (other.gameObject.CompareTag("RightHand"))
|
||
{
|
||
|
||
SceneMgr.Instance.isTrigger = false;
|
||
|
||
}
|
||
}
|
||
private void OnTriggerStay(Collider other)
|
||
{
|
||
if (other.gameObject.CompareTag("RightHand"))
|
||
{
|
||
//Debug.Log("Stay<61><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"+ other.gameObject.name);
|
||
m_catch = other.GetComponent<CatchManager>();
|
||
|
||
if (m_catch == null) return;
|
||
//Debug.Log("222<32>Ƿ<EFBFBD>ʼ:" + 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("<22><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>:"+isMove);
|
||
this.GetComponent<UpDownMovement>().isMove = isMove;
|
||
}
|
||
}
|
||
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
|
||
[SerializeField] private GameObject childAnimator; // ֱ<><D6B1><EFBFBD>϶<EFBFBD><CFB6><EFBFBD>
|
||
|
||
public void SetTriggerParameter(string parameterName)
|
||
{
|
||
if (childAnimator != null)
|
||
{
|
||
Destroy(childAnimator);
|
||
childAnimator = null;
|
||
}
|
||
}
|
||
|
||
}
|