c# - How can I avoid detection of second collision of same objects in Unity3d? -
i making game in style of old "lotus" games (player's car moves on bottom of screen left , right only, opponents car appear on top , "chased" move down towards player's car). have rigidbodies on cars , when player's car collides other 1 want detect but once.
i use:
void oncollisionenter (collision collision) { contactpoint contactpoint = collision.contacts [0]; gameobject cube = gameobject.createprimitive (primitivetype.cube); cube.transform.position = contactpoint.point; }
with code cube created 1-5 times , means collision occures several times on same object. achieve after first collision 1 not reported (no cube collision should created) still 1 cube should created when collision opponent car occurs. in short: 1 cube each collision opponent cars. intend use kind of score calculations.
any ideas?
i think should create bool
variable. private bool detectedcollision = false;
, when detect collision @ first turn variable true
, when you'll need detect collision turn false
. think easy way 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; } }
Comments
Post a Comment