- Home /
Box Colliders are moving from set location
Hi all,
I've been having an issue with some box colliders. I have these attached to an empty game object with no rigidbody component. This box collider sits inside of a larger sphere collider zone known as the "predator zone". When this zone is active, it also activates this box collider zone called "Too Close Zone" - if the player gets too close to this zone a warning pops up and a death timer is started based on if the player stays in that zone too long. I'm having a strange issue though where these box colliders are moving from their set locations. When starting gameplay they will often move from their set spots. There is no code making the game object move and there is no rigidbody attached to make the game object move so I'm confused as to why I'm having this issue. I will paste the code attached to the tooClozeZone game objects here as well as show a picture of their layout within the hierarchy. When the player stays in the tooCloseZone too long the boolean dead is set true which activates the deathTimer function attached to the larger predatorZone script. First is the tooCloseZone script and second pasted is the PredatorZone deathTimer function:
public GameObject fearSprites;
public static bool dead;
//public Vector3 zonePosition;
private float timeOfEntry, timeInZone, maxTimeInZone;
void Start () {
//zonePosition = transform.position;
timeOfEntry = 0;
timeInZone = 0;
maxTimeInZone = 7.0f;
dead = false;
}
void OnTriggerEnter () {
timeOfEntry = Time.time;
Debug.Log (timeOfEntry);
}
void OnTriggerStay (Collider other) {
if (other.GetComponent<Collider>().tag == "Player") {
if (timeInZone == 0) {
StartCoroutine (deathTimer (maxTimeInZone));
Debug.Log ("I'm running with max time");
} else {
StartCoroutine (deathTimer (maxTimeInZone - timeInZone));
Debug.Log ("I'm running with differential time");
}
fearSprites.SetActive (true);
/*if (timeInZone > maxTimeInZone) {
Debug.Log ("You dead scallywag");
}*/
}
}
void OnTriggerExit (Collider other) {
if (other.GetComponent<Collider>().tag == "Player") {
timeInZone += Time.time - timeOfEntry;
fearSprites.SetActive (false);
}
}
IEnumerator deathTimer (float seconds) {
yield return new WaitForSeconds (seconds);
dead = true;
fearSprites.SetActive (false);
}
}
PredatorZone deathTimer function:
IEnumerator deathTimer (float escapeTime) { yield return new WaitForSeconds (escapeTime); gameOver.SetActive (true); TooCloseZone.dead = false; tooCloseZone.GetComponent ().fearSprites.SetActive (false); tooCloseZone.SetActive (false); audioTriggered = true; predatorAlert.text = ""; predatorSfx.Stop (); predatorImg.gameObject.SetActive (false); gameObject.SetActive (false); }
IEnumerator deathTimer (float escapeTime) { yield return new WaitForSeconds (escapeTime); gameOver.SetActive (true); TooCloseZone.dead = false; tooCloseZone.GetComponent ().fearSprites.SetActive (false); tooCloseZone.SetActive (false); audioTriggered = true; predatorAlert.text = ""; predatorSfx.Stop (); predatorImg.gameObject.SetActive (false); gameObject.SetActive (false); }
IEnumerator deathTimer is the script
Hey, did you end up finding an answer to this issue? I've been running into the same problem, and it's the strangest thing. On all objects I have a box collider on, the collider seems to shift upwards the moment I press play.
Answer by sloththegarry · Dec 23, 2018 at 06:23 PM
Are you using a perspective camera? if so set the Z position of your Colliders and their corresponding gameobjects to zero, otherwise they move based on the perspective camera's frustum as your player reaches the edges of camera's FoV.
I've edited the answer to make it clearer ;)
That is helpful thank you. I set the box collider's center z transformation to 0, but I wouldn't want to set the z transform of the game object to 0 since I want it to be at a specific position on the map. Despite setting the box collider's z transform center to 0 I still see the same problem. Even when setting the z transform of the game object to 0 the collider still moves as well. So this may not be the solution - I'd love any other suggestions you may have!
I can't find anything that might be affecting the transform of your Box Colliders in your script, so it's possible that the cause stems from somewhere else in your project. I suggest you do add RigidBodies to your Colliders, Set the Is $$anonymous$$inematic boolean and clear Use Gravity boolean. This might solve the issue if something else in your project isn't causing it. Other than that, I would also rewrite this part of your code, it's somewhat irregular:
if (other.GetComponent<Collider>().tag == "Player")
Since you're already specifying a collider called "other" in the parameter of OnTriggerStay (Collider other), it's redundant to use GetComponent. Try the following:
if (other.gameObject.tag == "Player")
I don't mean to be a "grammar-nazi" but you never truly know what could be causing issues in your game :D Also try sending a snapshot of your project's hierarchy. Cheers
Thanks for the coding tip - I knew that was probably not the most efficient way to code it! I tried adding a rigid body and checking only is$$anonymous$$inematic but still have the same issue. Here is a snapshot of my hierarchy:
Your answer
Follow this Question
Related Questions
Physics2D, Box Collider physics inaccuracy? 1 Answer
Character Collider doesn't collide with other Colliders 2 Answers
How to detect which side of a box collider was hit? (Top, Bottom, Right, Left) 2D game/ C# 3 Answers
How can I disable this collider, when two are present on the gameobject 1 Answer
Textures and effects not in library 2 Answers