空气曲棍球游戏 - 玩家蝙蝠经过冰球,如果移动速度太快 [英] Air hockey game - Player bat goes through puck if moved too fast

查看:216
本文介绍了空气曲棍球游戏 - 玩家蝙蝠经过冰球,如果移动速度太快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯目前正在开发的Unity3d空气曲棍球游戏。我遇到的问题是,当玩家试图太快打冰球,玩家最终通过冰球去,因此没有碰撞。游戏完美的作品,如果玩家保持不动如预期和冰球击中玩家或者玩家击中以缓慢的步伐冰球。

Im currently developing an Air hockey game in Unity3d. The issue I'm having is that when the player attempts to hit the puck too quickly, the player ends up going through the puck and therefore there is no collision. The game works perfectly as expected if the player stays still and the puck hits the player or if the player hits the puck at a slow pace.

玩家有使用刚体使用胶囊对撞机连续碰撞检测。冰球也有刚体连续动态碰撞检测和凸网格对撞机。

The player has a rigidbody using continuous collision detection using a capsule collider. The puck also has rigidbody with continuous dynamic collision detection and a mesh collider with convex.

我试过固定时间步长设定为0.01,但没有产生效果。这里是玩家运动的脚本:

I tried setting the fixed timestep to 0.01 but that didn't have an effect. Here is the script for the player movement:

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;
}

和这里是冰球的代码时,它与玩家碰撞:

and here is the code for the puck when it collides with the player:

// 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");
}



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

Any help would be much appreciated. Thanks.

推荐答案

您是对的,试图连续碰撞检测(CCD)。有一些限制(尤其是在这种情况下要使用的CCD有两个移动的物体而不是一个移动物体和一个静态对象),但它是专为这种情况的。 的刚体文档进入这些约束:

You were right to try continuous collision detection (CCD). There are some constraints (especially in this case where you want to use CCD with two moving objects rather than one moving object and one static object), but it is designed for this kind of scenario. The Rigidbody documentation goes into these constraints:

设置碰撞检测模式为连续防止
刚体穿过任何静态(即非刚体)
MeshColliders。将它设置为连续动态也防止
刚体穿过设置为连续或连续动态
碰撞检测模式任何其他支持刚体。在支持盒 - ,Sphere-和
CapsuleColliders
连续碰撞检测。

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.

要综上所述,无论是冰球和桨需要被设置为连续动态,并且既需要是盒 - ,Sphere-或胶囊撞机。如果你可以让这些约束你的游戏工作,你应该能够得到连续碰撞检测,而不自己写的吧。

To sum up, both puck and paddle need to be set to Continuous Dynamic, and both need to be Box-, Sphere-, or Capsule Colliders. If you can make these constraints work for your game you should be able to get continuous collision detection without writing it yourself.

关于Unity的CCD一张纸条,值得重复:

A note about Unity's CCD that bears repeating:

注意,连续碰撞检测的目的是作为一个安全网
赶上碰撞中情况下,对象本来互相传递
,但不会提供精确物理碰撞
的结果,所以你可能还在考虑在TimeManager督察降低固定时间步长
值,使模拟更多
精确,如果您遇到的问题与快速移动的物体。

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.

不过,因为你是手动指定碰撞反应,可能不会成为一个问题。

But since you are manually specifying the collision reaction, that might not be an issue.

这篇关于空气曲棍球游戏 - 玩家蝙蝠经过冰球,如果移动速度太快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆