Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rkaundinya · Dec 23, 2018 at 09:37 AM · colliderbox collider

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); 
 }

}

  1. 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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Dec 23, 2018 at 11:08 AM 0
Share

please format everything correctly

avatar image evelynchin · Jun 25, 2020 at 07:55 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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 ;)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rkaundinya · Dec 24, 2018 at 12:53 AM 0
Share

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!

avatar image sloththegarry rkaundinya · Dec 24, 2018 at 12:43 PM 0
Share

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

avatar image rkaundinya sloththegarry · Dec 27, 2018 at 02:06 AM 0
Share

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:alt text

screen-shot-2018-12-26-at-60502-pm.png (28.0 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges