- Home /
Question by
akstheway · Oct 10, 2014 at 09:07 AM ·
collision detection
How to avoid one Collision when object collide between two collision Object
Hi,
I have four cubes which are close to each other. when my ball hit between two cube. OncollisionEnter Function detect twice at the same time and my score increase twice in single hit. all cubes are tag with same name.
How to solve this issue?
void OnCollisionEnter(Collision collisionObject)
{
if(collisionObject.gameObject.tag == "Cube" )
{
string points = collisionObject.gameObject.name;
switch(points)
{
case "Cube1":
gameDataRef.score += 100;
break;
case "Cube2":
gameDataRef.score += 50;
break;
case "Cube3":
gameDataRef.score += 60;
break;
case "Cube4":
gameDataRef.HitScore += 80;
break;
}
}
}
Comment
Best Answer
Answer by bubzy · Oct 10, 2014 at 09:18 AM
you can set a Boolean value for collision
bool hitCube = false
void OnCollisionEnter(Collision collisionObject)
{
if(!hitCube) //pointless checking this if we already hit one
{
if (collisionObject.gameObject.tag == "cube")
{
//do your switch(points) stuff here
hitCube = true;
}
}
}
void OnCollisionExit(Collision collisionObject)
{
if(hitCube&&collisionObject.gameObject.tag == "cube")
{
hitCube = false;
}
}
Your answer
Follow this Question
Related Questions
Prevent a fast moving ball from passing through a wall 2 Answers
contuouse collision detection malfunction 2 Answers
How to get rid of jumpy collisions? 1 Answer
Object collisions failing with fast movement 1 Answer
OnParticleCollision doesn´t work 0 Answers