本案例是使用animator 控制动画,角色死亡时触发 ragdoll 系统
首先是配置
配置完成后 使用脚本控制animator 动画和 ragdoll 系统的冲突
/// <summary>
/// ragdoll 的所有刚体
/// </summary>
List<Rigidbody> ragdollRigidbodys = new List<Rigidbody>();
/// <summary>
/// ragdoll 所有碰撞盒
/// </summary>
List<Collider> ragdollColliders = new List<Collider>();
/// <summary>
/// 使用animator动画时关闭布娃娃系统
/// </summary>
public void InitRagdoll()
{
Rigidbody[] _rigidbodys = GetComponentsInChildren<Rigidbody>();
for (int i = 0; i < _rigidbodys.Length; i++)
{
if (_rigidbodys[i] == GetComponent<Rigidbody>())
continue;
ragdollRigidbodys.Add(_rigidbodys[i]);
_rigidbodys[i].isKinematic = true;
Collider _collider = _rigidbodys[i].gameObject.GetComponent<Collider>();
_collider.isTrigger = true;
ragdollColliders.Add(_collider);
}
}
/// <summary>
/// 角色死亡时 开启ragdoll
/// </summary>
public void EnableRagdoll()
{
for (int i = 0; i < ragdollRigidbodys.Count; i++)
{
ragdollRigidbodys[i].isKinematic = false;
ragdollColliders[i].isTrigger = false;
}
Collider collidercontroller = GetComponent<Rigidbody>().gameObject.GetComponent<Collider>();
collidercontroller.enabled = false;
GetComponent<Rigidbody>().isKinematic = true;
animator.enabled = false;
this.enabled = false;
}