- Home /
Destroying objects within collision
I am having a problem when destroying an object while it is within a collider. I'm not trying to have the collider destroy the object.
Think of TF2, Overwatch, or Paladins. The Capture The Point type of gameplay is what I am working on... but if a character is still within the capture point and is destroyed, it is still being counted as being on point.
I have two scripts I am trying to work with on this: This is the script on the Objective Point
public float redCapCols;
public float blueCapCols;
private void OnTriggerEnter(Collider col){
if (col.tag == "TeamBlue"){
blueCapCols++;
}
if (col.tag == "TeamRed"){
redCapCols++;
}
}
This is the script on the player
public GameObject capPoint;
public float redCapCol;
public float blueCapCol;
public float curHealth;
void Start(){
CapPoint capturePoint = capPoint.GetComponent<CapPoint>();
redCapCol = capturePoint.redCapCols;
blueCapCol = capturePoint.blueCapCols;
}
private void OnTriggerStay (Collider col){
if (col.tag == "objective" && this.tag == "TeamRed"){
if(curHealth <= 0){
redCapCol--;
}
}
if (col.tag == "objective" && this.tag == "TeamBlue"){
if (curHealth <= 0){
blueCapCol--;
}
}
The Character Script also has code that will destroy it and whatnot when curHealth <= 0, but when the player is destroyed The CapCol(s) will keep a counter even with the Player gone.
In the player Script I have tried OnTriggerEnter, OnTriggerStay, and OnTriggerExit, cannot find how to fix this problem.
Answer by etopsirhc · Aug 02, 2017 at 04:43 AM
in the character keep a variable of the current capture point it's on. then add a
void OnDestroy() {
thatCollider.OnTriggerExit( /* the characters collider */ );
}
that should fix the problem entirely.