Unity3d 物体不动,碰撞触发不了的情况

问题:
有一种情况,主角带刚体,主角站着不动。玩家站在陷阱上,陷阱的碰撞体 Toggle 之后,OnCollisionEnter 触发不了。

解决:盲猜玩家组件上才有刚体,而碰撞检测是刚体运动的时候,才进行检测的。

private void OnBecameVisible()
{
    if (_trapBase is TrapSpike)
    {
        InvokeRepeating(nameof(CheckPlayerStay), 0, 0.1f);
    }
}

private void OnBecameInvisible()
{
    CancelInvoke(nameof(CheckPlayerStay));            
}

/// 
/// 地刺的碰撞体会消失和隐藏,特殊处理。
/// 
public void CheckPlayerStay()
{
    LeadView leadView = RoleManagerView.instance?.Lead;
    if (!leadView)
    {
        return;
    }
        
    if (!_collider.bounds.Contains(leadView.transform.position)
        && !_collider.bounds.Contains(leadView.transform.position + 0.1f * Vector3.up)
        && !_collider.bounds.Contains(leadView.transform.position + Vector3.up))
    {
        var pos = _trapBase.transform.position;
        pos.y = 0;
        Collider[] colliders = Physics.OverlapSphere(pos, 2, 1 << Layers.Player); 
        if (colliders.Length <= 0)
            return;
    }

    leadView.Rigid.velocity += 0.00001f * Vector3.up;
    
    MakePlayerFallGround();
}

protected void MakePlayerFallGround()
{
    if (_leadView)
    {
        if (Physics.Raycast(_leadView.transform.position, Vector3.down, out RaycastHit hit, 3, 1 << Layers.Ground | 1 << Layers.Default))
        {  
            if (hit.distance >= 0.01f)
            {
                _leadView.transform.DOMoveY(hit.point.y, 1f).SetEase(Ease.InQuad);
            }
        }
    }
}

你可能感兴趣的:(unity,问题记录,unity)