简体   繁体   English

如何避免在Unity3d中检测到相同对象的第二次碰撞?

[英]How can I avoid detection of second collision of same objects in Unity3d?

I am making a game in the style of old "Lotus" games (player's car moves on the bottom of the screen left and right only, opponents car appear on top and are "chased" but actually just move down towards player's car). 我正在按照旧的“莲花”游戏风格制作游戏(玩家的赛车仅在屏幕底部左右移动,对手的赛车出现在顶部并被“追赶”,但实际上只是向下移动到玩家的赛车)。 So I have rigidbodies on all cars and when player's car collides with other one I want to detect it but only once . 因此,我在所有汽车上都有刚体,当玩家的汽车与其他汽车发生碰撞时,我只想检测一次

I use: 我用:

void OnCollisionEnter (Collision collision)
{
    ContactPoint contactPoint = collision.contacts [0];

    GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
    cube.transform.position = contactPoint.point;

}

With this code cube is created 1-5 times and that means that collision occures several times on same object. 使用此代码立方体将被创建1-5次,这意味着在同一对象上发生多次碰撞。 What I would like to achieve is that after first collision another one is not reported (no cube for this collision should be created) but still one cube should be created when collision with another opponent car occurs. 我要实现的是,第一次碰撞后没有报告另一个碰撞(不应创建用于该碰撞的多维数据集),但是当与另一辆对手汽车发生碰撞时仍应创建一个多维数据集。 In short: only one cube for each collision with opponent cars. 简而言之:与对手汽车的每次碰撞只有一个立方体。 I intend to use it for kind of score calculations. 我打算将其用于某种分数计算。

Any ideas? 有任何想法吗?

I think that you should create a bool variable. 我认为您应该创建一个bool变量。 private bool detectedCollision = false; And than when you detect a collision at first you turn this variable to true and when you'll need to detect another collision just turn it back to false . 并且比起首先检测到碰撞时,您可以将此变量设置为true而当您需要检测另一次碰撞时,只需将其重新设置为false I think this is an easy way to do it. 我认为这是一种简单的方法。

void OnCollisionEnter (Collision collision)
{
if(!detectedCollision){
    ContactPoint contactPoint = collision.contacts [0];

    GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
    cube.transform.position = contactPoint.point;
detectedCollision = true;
} 
}

Whatever class is keeping score, have a master list for collisions where you would then do: 无论哪个班保持得分,都有一个碰撞主列表,您将在该列表中进行以下操作:

class CarCollision
{
    public string[] victims;

    public CarCollision(params string[] Victims)
    {
        victims = Victims;
    }
}

... ...

void OnCollisionEnter (Collision col)
{
    masterList.AddCollission(collision.contacts [0], this.name, col.gameObject.name);
}

void OnCollisionExit (Collision col)
{
    masterList.RemoveCollision(this.name, col.gameObject.name);
}

... ...

class ScoreClass
{
    private List<CarCollision> collisions = new List<CarCollision>();

    public void AddCollision(ContactPoint contactPoint, params string[] victims)
    {
        if(!collisions.Any(i => 
            i.victims.All(v => victims.Contains(v)))
        {
            collisions.Add(new CarCollision(victims));

            GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
            cube.transform.position = contactPoint.point;
        }
    }

    public void RemoveCollision(params string[] victims)
    {
        collisions.RemoveAll(i => 
            i.victims.All(v => victims.Contains(v)));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM