53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using BITKit;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.Animations;
|
||
|
|
||
|
namespace BITFALL.Props
|
||
|
{
|
||
|
public class Prop_ReplaceOnSleep : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] private Transform prefab;
|
||
|
[SerializeField] private new Rigidbody rigidbody;
|
||
|
private readonly IntervalUpdate _interval = new(0.16f);
|
||
|
private Collider _collider;
|
||
|
private void FixedUpdate()
|
||
|
{
|
||
|
if (rigidbody.IsSleeping() && _interval.AllowUpdate)
|
||
|
{
|
||
|
ReplaceImmediate(_collider.transform);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void ReplaceImmediate(Transform root=null)
|
||
|
{
|
||
|
var _transform = transform;
|
||
|
var instance = Instantiate(prefab);
|
||
|
instance.SetPositionAndRotation(_transform.position, _transform.rotation);
|
||
|
|
||
|
if (root is not null)
|
||
|
{
|
||
|
instance.SetParentConstraint(root);
|
||
|
if (instance.TryGetComponent<Rigidbody>(out var _rigidbody))
|
||
|
{
|
||
|
//_rigidbody.isKinematic = true;
|
||
|
_rigidbody.isKinematic = true;
|
||
|
}
|
||
|
}
|
||
|
Destroy(gameObject);
|
||
|
}
|
||
|
|
||
|
private void OnCollisionEnter(Collision other)
|
||
|
{
|
||
|
_collider = other.collider;
|
||
|
}
|
||
|
|
||
|
private void OnCollisionStay(Collision other)
|
||
|
{
|
||
|
_collider = other.collider;
|
||
|
}
|
||
|
}
|
||
|
}
|