You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.5 KiB
106 lines
2.5 KiB
10 months ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using Unity.MLAgents;
|
||
|
using Unity.MLAgentsExamples;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.Events;
|
||
|
|
||
|
public class OnChairContact : MonoBehaviour
|
||
|
{
|
||
|
public UnityEvent m_CollisionEvent;
|
||
|
public UnityEvent m_CollisionStayEvent;
|
||
|
public UnityEvent m_CollisionExitEvent;
|
||
|
|
||
|
public UnityEvent m_TriggerStayEvent;
|
||
|
public UnityEvent m_TriggerExitEvent;
|
||
|
|
||
|
public bool m_IsTouching;
|
||
|
public bool m_IsAttached;
|
||
|
public float m_TrayTimeout;
|
||
|
private string m_Tag = "agent";
|
||
|
|
||
|
[SerializeField]
|
||
|
public FixedJoint m_FixedJoint;
|
||
|
|
||
|
|
||
|
void OnEnable()
|
||
|
{
|
||
|
m_IsTouching = false;
|
||
|
m_IsAttached = false;
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
if (this.TryGetComponent<FixedJoint>(out FixedJoint component))
|
||
|
{
|
||
|
m_FixedJoint = component;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
m_FixedJoint = null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnCollisionEnter(Collision col)
|
||
|
{
|
||
|
if (col.transform.CompareTag(m_Tag))
|
||
|
{
|
||
|
if (col.gameObject.GetComponent<GroundContact>().isFoot && m_FixedJoint && !m_IsAttached)
|
||
|
{
|
||
|
m_IsAttached = true;
|
||
|
// Attach Joint
|
||
|
Rigidbody rbFoot = col.gameObject.GetComponent<Rigidbody>();
|
||
|
Debug.Log($"Attached to {col.gameObject.name}");
|
||
|
m_FixedJoint.connectedBody = rbFoot;
|
||
|
StartCoroutine(WaitToDetatch());
|
||
|
}
|
||
|
|
||
|
m_IsTouching = true;
|
||
|
m_CollisionEvent?.Invoke();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnCollisionStay(Collision col)
|
||
|
{
|
||
|
if (col.transform.CompareTag(m_Tag))
|
||
|
{
|
||
|
m_IsTouching = true;
|
||
|
m_CollisionStayEvent?.Invoke();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnCollisionExit(Collision other)
|
||
|
{
|
||
|
if (other.transform.CompareTag(m_Tag))
|
||
|
{
|
||
|
m_IsTouching = false;
|
||
|
m_CollisionExitEvent?.Invoke();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnTriggerStay(Collider other)
|
||
|
{
|
||
|
if (other.transform.CompareTag(m_Tag))
|
||
|
{
|
||
|
m_IsTouching = true;
|
||
|
m_TriggerStayEvent?.Invoke();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnTriggerExit(Collider other)
|
||
|
{
|
||
|
if (other.transform.CompareTag(m_Tag))
|
||
|
{
|
||
|
m_IsTouching = false;
|
||
|
m_TriggerExitEvent?.Invoke();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
IEnumerator WaitToDetatch()
|
||
|
{
|
||
|
Debug.Log("Waiting for detatch");
|
||
|
yield return new WaitForSeconds(m_TrayTimeout);
|
||
|
Destroy(m_FixedJoint);
|
||
|
}
|
||
|
}
|