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 /
  • Help Room /
avatar image
0
Question by Janjanus · Oct 09, 2015 at 10:42 AM · positionareacontainsrectangle

Check if a position is inside a rectangular shaped area?

I need to now if a certain position is inside a certain rectangular shaped area. I have looked inside the docs for something like this and I came across this: Rect.Contains() and this BoxColider.

Rect.Contains only works for 2D spaces and the BoxColider seems a bit like a over kill for my scenario since I only need to know if a certain position x is inside area y. So I am thinking of implementing my own class. I have the following structure in mind:

 RectangleArea r = new RectangleArea(new Vector3(1, 1, 1), Quaternion.Euler(0, 45, 0), 10, 10, 10);
 
 if(r.Contains(new Vector3(1, 1, 1))) {
    // We are inside
 }

The RectangleArea needs a position a rotation and a width, height and depth. With these parameters the class will construct its 3D rectangular shaped area and I can call the Contains method on it.

  • Question 1: should I implement my own class? Or is there something available in Unity that I am missing?

  • Question 2: I am having a hard time figuring out the maths behind the RectanleArea class. The first thing is to construct a rectangular area in 3D space from a: position, rotation, width, height and dept how can i do that? The second part is the Contains method how can i see is a position is inside the area?

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

1 Reply

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

Answer by MadDevil · Oct 09, 2015 at 11:08 AM

@anjanus

you can use bound function provided by unity, to check whether your point is inside a rectangular space or not.

http://docs.unity3d.com/ScriptReference/Bounds.html

Comment
Add comment · Show 4 · 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 Janjanus · Oct 09, 2015 at 12:53 PM 0
Share

Thank you for your response, I have looked at the Bounds struct and it seems it should work for my problem. But how do I use this struct? I have the position rotation and the width, height and depth of the area how do I make a Bounds object of it? Since Bounds only accept a center and a size vector?

avatar image MadDevil Janjanus · Oct 12, 2015 at 04:37 AM 0
Share

you can make a box collider with specific size and position as your requirements, and then you can check collider.bounds to know if the object is inside or not.

void Update() { if(transform.collider.bounds.Contains(_point.position)) { print("Inside"); } }

avatar image Janjanus MadDevil · Oct 12, 2015 at 08:15 AM 0
Share

I don't have access to a Transform so I need to make the Bounds object myself. Do you have any clue on how to do that with the parameters I got?

Show more comments

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

31 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

Related Questions

Drawing a rectangle on gameObject's position 2 Answers

List.Contains not working as intended 0 Answers

Spawn objects in a row with same spacing 0 Answers

What is different when Instantiate set Prefab Position? 1 Answer

Unity zPosition hierarchy is making me insane. 0 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