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 siddharth3322 · May 29, 2014 at 06:14 AM · gameobjecttriggeralgorithmunity4.3

GameObjects creation within boundry

For my 2d game, I want to certain game objects with certain area at random position. For this I have added following type of calculation.

 float xPosition = Random.Range (minHorizontalRange, maxHorizontalRange);
 float yPosition = Random.Range (minVerticleRange, maxVerticleRange);

And it got generated at random position. But at this point I want some game objects get overlapped by other one. So I want to remove this problem for my game.

For this I have added following code

 void OnTriggerEnter2D (Collider2D other)
     {
         if (!other.CompareTag (Constants.TAG_BALL)) {
             // place game object at some other position
         }
     }

But overlapped object don't detect OnTriggerEnter2D() method gets called.

So basically here I define my requirements and problem, provide some help in this. If you have any better algorithm for this then please suggest me.

Comment
Add comment · Show 1
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 siddharth3322 · May 29, 2014 at 06:52 AM 0
Share

$$anonymous$$y inspector window for each created game object.

alt text

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by siddharth3322 · May 29, 2014 at 11:32 AM

I can able to solve this problem using other way. OnCollisionEnter2D() and OnTriggerEnter2D() methods didn't called at all.

I used Physics2D.OverlapCircleAll() method to solve this problem. Here is my code snippet that work for me and thanks for you suggestions.

 private void CheckForOverlap (GameObject holeInstance)
     {
         Collider2D [] otherColliders = Physics2D.OverlapCircleAll (new Vector2 (holeInstance.transform.position.x, holeInstance.transform.position.y), 1.5f);
         if (otherColliders.Length > 1) {
             float xPosition = Random.Range (minHorizontalRange, maxHorizontalRange);
             float yPosition = Random.Range (minVerticleRange, maxVerticleRange);
             float zPosition = -1f;
             
             Vector3 holePosition = new Vector3 (xPosition, yPosition, zPosition);
             holeInstance.transform.position = holePosition;
             CheckForOverlap (holeInstance);
         }
     }
Comment
Add comment · 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
0

Answer by wijesijp · May 29, 2014 at 06:31 AM

after getting the position do a test to see whether there are any objects within range You can use Physics2D.OverlapCircle to test

But this is a costly operation.

Comment
Add comment · Show 6 · 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 siddharth3322 · May 29, 2014 at 06:35 AM 0
Share

I can't able to understand why my OnTriggerEnter2d() method not called? Any idea you have. Then I don't have to perform any extra stuff.

avatar image wijesijp · May 29, 2014 at 06:41 AM 0
Share

You do have rigid body 2D added to your game objects right?

avatar image wijesijp · May 29, 2014 at 06:44 AM 0
Share

anyway you need to do some test before spawning right? Even if OnTriggerEnter2D worked what are you going to do now? Objects are created and they are overlapping ! Are you going to move them or delete them?

avatar image siddharth3322 · May 29, 2014 at 06:51 AM 0
Share

GameObject has rigidbody component attached already. I want to move same object at other position.

avatar image wijesijp · May 29, 2014 at 06:57 AM 0
Share

I think the problem may be
if (!other.CompareTag (Constants.TAG_BALL))

it is saying if compare tag fails do something. Just a guess I don't know how you setup your objects

Show more comments
avatar image
0

Answer by Noob_Vulcan · May 29, 2014 at 06:41 AM

Change OnTriggerEnter2D() to OnTriggerStay2D() . Try might work

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 siddharth3322 · May 29, 2014 at 06:50 AM 0
Share

I have already tested OnTriggerStay2D but didn't worked.

avatar image Noob_Vulcan · May 29, 2014 at 07:03 AM 0
Share

k.. I think non of the things will get called trigger/collision . Since your objects spawn inside other (half or full). You will have to manage via code. As far as i know

avatar image siddharth3322 · May 29, 2014 at 07:10 AM 0
Share

Thanks @Noob_Vulcan for your suggestions.

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

22 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

Related Questions

At same positioned game objects collision detection 0 Answers

Game Development Approach 0 Answers

Access Prefab Object Via Scirpt 2 Answers

Game AI Implementation 0 Answers

Object dragging at manual point 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