Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Adyrhan · Nov 20, 2014 at 08:17 PM · transformtriggerboxcollider

Instantiating and deleting objects with triggers problem

I'm trying to create some kind of dust field, by now I'm just creating small balls for testing, between the bounds of a BoxCollider which I use as my dust field. When the box collider goes outside the collider of one of the dust balls, it triggers the OnTriggerExit of that ball and creates a new instance of the ball in a random position at the front and removes the current instance.

The problem I have is that I find some of the balls on the sides of the box (along the x axis) not colliding with the dust field's BoxCollider as soon as the object with the BoxCollider starts to change direction. Since it doesn't collide, it also doesn't call OnColliderExit, and I need to call for the creation of a new dust object and the deletion of the current one at that point. So if I watch on the editor, I can see ball objects leaking on the sides of the box.

I've tried to use some limit to the bounds of the box for the dust object placement (limitPerAxis variable in code) but only works well when using something like 0.5 or 0.6 (50% - 60%) of the area of the BoxCollider, but doing that only makes the dust balls to appear in bursts because of the space left.

UPDATE

I've changed the rotation of the player object which is the parent of the box, and now I can see the balls get placed outside of the box when I set the Y rotation of the parent object to something like 45, or anything in between 0 and 90, 90 and 180 and such, even when created on Start event, so they get the chance of never collide with it. I've been checking that all sizes that might be used are set to 1 just in case. I'll upload some pictures so I can show better what I mean.

Here are the balls created inside the box when the player Y rotation is set at 0: alt text

And this is what happens when I set Y rotation to 45. Balls are located outside the box. It seems like they are rotated by TransformPoint call, but also, like the player transform size components where bigger than 1, which are all 1: alt text

This is how I create the objects:

 public class DustFieldCreator : MonoBehaviour {
 
     // Use this for initialization
     public GameObject DustObject;
     public GameObject World;
     public int Particles;
     private BoxCollider boxCollider;
     private const float limitPerAxis = 0.75f; // Ratio
 
     void Start () {
         boxCollider = GetComponent<BoxCollider>();
         createField();
     }
 
     private void createField()
     {
         for (int i = 0; i < Particles; i++)
         {
             float xpos = Random.Range(((boxCollider.bounds.size.x / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.x / 2) * limitPerAxis);
             float ypos = Random.Range(((boxCollider.bounds.size.y / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.y / 2) * limitPerAxis);
             float zpos = Random.Range(((boxCollider.bounds.size.z / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.z / 2) * limitPerAxis);
 
             createParticle(xpos, ypos, zpos);
         }
     }
 
     private void createParticle(float x, float y, float z)
     {
         GameObject dustInstance = (GameObject)GameObject.Instantiate(DustObject);
         dustInstance.transform.parent = World.transform;
 
         Vector3 dustPosition = new Vector3(x, y, z);
         dustPosition = transform.TransformPoint(dustPosition);
         dustInstance.transform.localPosition = dustPosition;
 
         DustBall ball = dustInstance.GetComponent<DustBall>();
         ball.DustField = gameObject;
     }
 
     public void createParticleInFront()
     {
         float xpos = Random.Range(((boxCollider.bounds.size.x / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.x / 2) * limitPerAxis);
         float ypos = Random.Range(((boxCollider.bounds.size.y / 2) * -1) * limitPerAxis, (boxCollider.bounds.size.y / 2) * limitPerAxis);
         float zpos = (boxCollider.bounds.size.z / 2) * limitPerAxis;
 
         createParticle(xpos, ypos, zpos);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void OnGUI() 
     {
 
     }
 }

The dust ball method for OnTriggerExit:

     void OnTriggerExit(Collider collider)
     {
         if (collider.gameObject == DustField)
         {
             Debug.Log("Ball outside field");
 
             dustCreator.createParticleInFront();
             GameObject.Destroy(gameObject);
         }
     }

What is that I'm doing wrong?

insidebox.png (109.6 kB)
outsidebox.png (155.0 kB)
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Trigger not detecting tag 1 Answer

transform.position not executed with OnTrigger2D 1 Answer

What would you do with essential items? 1 Answer

Check if the center of an object is inside a trigger Collider? 1 Answer

RigidBody2D - Ignoring other RigidBodies. 1 Answer


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