问题:
有一种情况,主角带刚体,主角站着不动。玩家站在陷阱上,陷阱的碰撞体 Toggle 之后,OnCollisionEnter 触发不了。文章来源:https://www.toymoban.com/news/detail-735848.html
解决:盲猜玩家组件上才有刚体,而碰撞检测是刚体运动的时候,才进行检测的。文章来源地址https://www.toymoban.com/news/detail-735848.html
private void OnBecameVisible()
{
if (_trapBase is TrapSpike)
{
InvokeRepeating(nameof(CheckPlayerStay), 0, 0.1f);
}
}
private void OnBecameInvisible()
{
CancelInvoke(nameof(CheckPlayerStay));
}
/// <summary>
/// 地刺的碰撞体会消失和隐藏,特殊处理。
/// </summary>
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);
}
}
}
}
到了这里,关于Unity3d 物体不动,碰撞触发不了的情况的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!