简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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