c# – 空气曲棍球比赛 – 如果移动得太快,球员蝙蝠会经过冰球

前端之家收集整理的这篇文章主要介绍了c# – 空气曲棍球比赛 – 如果移动得太快,球员蝙蝠会经过冰球前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在Unity3d开发一款空气曲棍球比赛.我遇到的问题是,当玩家试图过快地击中冰球时,玩家最终会通过冰球,因此没有碰撞.如果玩家保持静止并且冰球击中玩家或者玩家以缓慢的速度击中冰球,则游戏将按照预期完美地运行.

玩家有一个使用胶囊对撞机进行连续碰撞检测的刚体.冰球还具有连续动态碰撞检测的刚体和带凸面的网格对撞机.

我尝试将固定时间步长设置为0.01,但没有效果.这是玩家运动的脚本:

void ObjectFollowCursor()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Vector3 point = ray.origin + (ray.direction * distance);

    Vector3 temp = point;
    temp.y = 0.2f; // limits player on y axis

    cursorObject.position = temp;
}

这是冰球与玩家碰撞时的代码

// If puck hits player
if(collision.gameObject.tag == "Player")
{
    Vector3 forceVec = this.GetComponent<Rigidbody>().velocity.normalized * hitForce;
    rb.AddForce(forceVec,ForceMode.Impulse);
    Debug.Log ("Player Hit");
}

任何帮助将非常感激.谢谢.

解决方法

你是正确的尝试连续碰撞检测(CCD).存在一些约束(特别是在这种情况下,您希望使用具有两个移动对象的CCD而不是一个移动对象和一个静态对象),但它是针对这种情况设计的. The Rigidbody documentation进入这些限制:

Set the collision detection mode to Continuous to prevent the
rigidbody from passing through any static (ie,non-rigidbody)
MeshColliders. Set it to Continuous Dynamic to also prevent the
rigidbody from passing through any other supported rigidbodies with
collision detection mode set to Continuous or Continuous Dynamic.
Continuous collision detection is supported for Box-,Sphere- and
CapsuleColliders.

总而言之,冰球和桨叶都需要设置为连续动态,并且都需要是Box,Sphere或Capsule Colliders.如果您可以使这些约束适用于您的游戏,您应该能够连续碰撞检测,而无需自己编写.

有关Unity的CCD的重复说明:

Note that continuous collision detection is intended as a safety net
to catch collisions in cases where objects would otherwise pass
through each other,but will not deliver physically accurate collision
results,so you might still consider decreasing the fixed Time step
value in the TimeManager inspector to make the simulation more
precise,if you run into problems with fast moving objects.

但由于您手动指定碰撞反应,这可能不是问题.

原文链接:https://www.f2er.com/csharp/239202.html

猜你在找的C#相关文章