ok so here is what I am trying to do… I am making a game where you throw objects at a board. If the objects hit the board you get a plus 1 to the score. if the objects gets knocked off you lose that point. I figured the score would be a simple thing to do if the bag and board collide increment the score by 1… the issue is that it never stops incrementing the score when the collision happens… I have tried using both onTriggerEnter and onCollisionEnter and neither one increments by just one. So I threw a Boolean function in so if it is true it will increment the score by one as well it also did not work… so now after two weeks of a bajillion different code variations at doing this I am reaching out to you guys for some assistance… can I get a working example of how to do this… I understand the unwillingness to just give answers… but I have been fighting with this for two weeks now and feel like I have all the pieces I am just not putting them in the right order… I have not posted the code because I feel like I need to start over with it, hopefully with a better understanding provided by your responses. Thanks in advance… Richard
Posting code is always a good idea, even when you feel you have to start over. It can tells us many things: what language you write in, the data structures you use, the relationship between game object (where you store the score for example in this probelm), etc.
I suspect your problem is that you are not check what you are colliding with. That would be that later hits by other projectiles might directly or indirectly cause OnCollisionEnter() calls resulting in score additions. Here is the bones of a script that would go on the projectiles. It expect the board to have the name ‘Board’, and the box or plane under the board, to be named ‘Floor’. Note the way this logic is structured, if the projectile first hits the board and then falls to the floor (or is knocked to the floor by another projectile), the score for that projectile would 0.
#pragma strict
bool hitboard = false;
bool hitfloor = false;
function OnCollisionEnter(col : Collision) {
if (!hitboard && col.gameObject.name == "Board") {
// Do whatever to add one to score
hitboard = true;
}
if (!hitfloor && col.gameObject.name == "Floor") {
// Do whatever to subtract one from score
hitfloor = true;
hitboard = true;
}
}
I don’t know in which manner you are using the Physics engine, but if the object really can be knockbacked from the board you could simply add an OnCollisionExit function to your collision script. If the two objects dont collide anymore do “score - 1” or sth. would be the easiest way i guess