【问题标题】:unity raycast hit calls three times when item dropped to the ground当物品掉到地上时,unity raycast hit call 3 次
【发布时间】:2019-04-26 11:08:26
【问题描述】:
每次触地时,我从球到地面的光线投射调用 3 次。
我只需要一次充值动画。
电话是这样的:
private void FixedUpdate()
{
if (!Physics.Raycast(transform.position, -Vector3.up, distanceground + 0.1f))
{
Debug.Log("intheair");
}
else {
dropped = true;
Debug.Log("dropped");
if (dropped && !GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsTag("topup"))
{
GetComponent<Animator>().SetTrigger("topup");
Debug.Log("trigged");
}
}
【问题讨论】:
标签:
unity3d
raycasting
game-development
【解决方案1】:
这可能会解决您的问题。
if (!Physics.Raycast(transform.position, -Vector3.up, distanceground + 0.1f) && !dropped)
{
Debug.Log("intheair");
}
else {
dropped = true;
Debug.Log("dropped");
if (dropped && !GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsTag("topup"))
{
GetComponent<Animator>().SetTrigger("topup");
Debug.Log("trigged");
}
}
【解决方案2】:
一旦你的对象在
distanceground + 0.1f
然后
if (!Physics.Raycast(transform.position, -Vector3.up, distanceground + 0.1f))
将在每个 FixedUpdate() 中返回 false 并遵循您的 else 块,因此问题不在于 Raycast。
问题很可能在于您在FixedUpdate() 中检查GetCurrentAnimatorStateInfo(0)。在较低的帧率下,FixedUpdate() 可以为每个 Update() 调用多次,导致
if (dropped && !GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsTag("topup"))
评估true,因为视觉动画状态可能还没有时间更新。
我建议将其全部移至Update()。