- Home /
game issues with infinte collisions
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
onTriggerEnter, onCollisionEnter?
OnTriggerEnter, OnCollisionEnter :)
Guessing thats not really your problem, I just thought I'd mention it.
ok here is my code if it will help it might be more confusing lol...
// this code is on the board
using UnityEngine;
using System.Collections;
public class ID355 : $$anonymous$$onoBehaviour
{
public GameObject score; //reference to the ScoreText gameobject, set in editor
public GameObject otherObject;
public bool okToScore = true;
public int redScore;
public int redTurn;
public int scoreRed = 0;
public GameObject isTouched;
void OnTriggerEnter(Collider col) {
bool touch$$anonymous$$e = isTouched.GetComponent<floorPlane> ().isTouched;
if (col.gameObject.tag == "bagCloneRed" && okToScore == false && touch$$anonymous$$e == false) {
otherObject.GetComponent<bagDetect> ().canScore = false;
okToScore = true;
redTurn = redTurn +1;
otherObject.GetComponent<bagDetect> ().canScore = false;
}
if (col.gameObject.tag == "bagCloneRed" && okToScore == false && touch$$anonymous$$e == true) {
otherObject.GetComponent<bagDetect> ().canScore = false;
okToScore = false;
redTurn = redTurn +1;
otherObject.GetComponent<bagDetect> ().canScore = true;
}
}
void Update (){
if (redTurn == 4) {
print (scoreRed);
int currentScore = int.Parse (score.GetComponent<GUIText> ().text) + scoreRed; //add 1 to the score
score.GetComponent<GUIText> ().text = currentScore.ToString ();
redTurn = 0;
scoreRed = 0;
}
}
}
and here is the rest well for some reason it is not letting me post the rest
//this code is on on the bag
using UnityEngine;
using System.Collections;
public class bagDetect : $$anonymous$$onoBehaviour {
public GameObject okToShoot;
public bool canScore;
public GameObject score;
public GameObject scoreRed;
public GameObject isTouched;
void Start() {
if (okToShoot.GetComponent<ID355> ().okToScore == true) {
okToShoot.GetComponent<ID355> ().okToScore = false;
scoreRed.GetComponent<ID355> ().scoreRed = scoreRed.GetComponent<ID355> ().scoreRed +1;
}
}
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "floorPlane") {
isTouched.GetComponent<floorPlane> ().isTouched = true;
}
}
}
//on the floor
using UnityEngine;
using System.Collections;
public class floorPlane : $$anonymous$$onoBehaviour {
public bool isTouched;
// Use this for initialization
void Start () {
isTouched = false;
}
// Update is called once per frame
void Update () {
}
}
Answer by robertbu · Aug 07, 2014 at 06:05 PM
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;
}
}
robertbu I will try that out and see how it works... it could be I have just over engineered the code
robertbu thanks for the help this answered the issue... i most defninately over engineered my code lol
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;
}
}
Answer by Foose · Aug 07, 2014 at 06:08 PM
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
Foose that makes sense but the score still continuously counts up while the object is touching it... in this case the board is at a very slight angle on the ground so the object a small beanbag in this case sits on it.