41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ShelfNudge : MonoBehaviour
|
|
{
|
|
[SerializeField] private float _nudgeForce = 1f;
|
|
[SerializeField] private float _waitTime = 1.5f;
|
|
|
|
private bool _isWaiting = false;
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.CompareTag("Target") && !_isWaiting)
|
|
StartCoroutine(NudgeIfStuck(other.GetComponent<Rigidbody>()));
|
|
}
|
|
|
|
private System.Collections.IEnumerator NudgeIfStuck(Rigidbody rb)
|
|
{
|
|
if (rb == null) { _isWaiting = false; yield break; }
|
|
_isWaiting = true;
|
|
yield return new WaitForSeconds(_waitTime);
|
|
|
|
if (rb != null && rb.velocity.magnitude < 0.01f)
|
|
{
|
|
rb.WakeUp();
|
|
Vector3 shelfNormal = transform.parent.up;
|
|
Vector3 gravity = Vector3.down;
|
|
Vector3 slideDirection = gravity - Vector3.Dot(gravity, shelfNormal) * shelfNormal;
|
|
slideDirection.Normalize();
|
|
|
|
float timer = 0f;
|
|
while (timer < 1f && rb != null && rb.velocity.magnitude < 0.5f)
|
|
{
|
|
rb.AddForce(slideDirection * _nudgeForce, ForceMode.Force);
|
|
timer += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
}
|
|
_isWaiting = false;
|
|
}
|
|
} |