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 jamespaxi · Jul 24, 2013 at 12:08 PM · gameobjecttextorthographicmap making

Detect if a GameObject is partially behind another?

Hi all,

I have a map with many "Label" GameObjects scattered around. Unfortunately a few cover each over, making it quite messy.

I think that a good strategy is to check if a GameObject is over or behind another, so that I just display one in that area.

Problem is I have no idea how to do start, and there is likely to be a straightforward way to accomplish this in Unity.


An idea would be to calculate the bounds of each individual Label and check for intersections.

I imagine it might end up being hell if I decide to tilt the Camera! ...and as it always happens Unity already offers a way to do this out of the box... :)


Just for the record, I'm using an Orthogonal Camera, which I imagine should make this a lot easier and all the labels are placed on the XY axis.


Thanks! Hopefully someone can help :)

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 prototype7 · Jul 24, 2013 at 12:18 PM 1
Share

Did you try this ?

 // detects if other transform is behind this object
     var other : Transform;
     function Update() {
         if (other) {
             var forward = transform.TransformDirection(Vector3.forward);
             var toOther = other.position - transform.position;
             if (Vector3.Dot(forward,toOther) < 0)
                 print ("The other transform is behind me!");
         }
     }
avatar image jamespaxi · Jul 24, 2013 at 12:25 PM 0
Share

@prototype7 Thanks for this! Though, wouldn't this return true for all Labels more distant from the Camera, rather than just the ones strictly behind the object?

1 Reply

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

Answer by Jamora · Jul 24, 2013 at 12:30 PM

You are correct in saying Unity offers a way out of the box. You can use Bounds.Intersects. You will need a (Box)collider on every GameObject, then gain access to the bounds with gameObject.collider.bounds. If you only needs the bounds for this intersection check, tick the isTrigger checkbox in the collider's inspector so it doesn't interact with other objects.

Depending on how many gameobjects you need to check, you might want to use Physics.OverlapSphere to only get the ones nearby. If you have hundreds of objects and you check each bounding box against all others, you will get lag.

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 jamespaxi · Jul 24, 2013 at 03:37 PM 0
Share

Thanks! This is really damn useful! :) Also just in case, are there any gotchas I should be aware of?

avatar image Jamora · Jul 24, 2013 at 03:52 PM 1
Share

Apart from the isTrigger checkbox, none that I can think of.

avatar image clunk47 · Jul 24, 2013 at 04:51 PM 0
Share

$$anonymous$$oving up in the community @Jamora, I've seen you posting some nice answers. $$anonymous$$eep it up :)

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

18 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

Related Questions

Quest Script Help 2 Answers

Generating Random Numbers on a Game Object 1 Answer

GetComponent, int error, if statement, problem. 1 Answer

Unity hangs on start when finding gameobject 0 Answers

text display on touch 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