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 mbvila · Jan 08, 2019 at 12:09 PM · gameobjecttransform.position

How to check if a GameObject is inside and area

Hello Guys, first of all, sorry if my Eglish is not the best, i'm not a native speaker.

I'm trying to detect if a gameObject is inside an area of the world. I have made an imaginary square to use it as the limits of the area.

I made an "If" to check if the transform.position.x,y,x of the gameObject and then I compared them with the limits of the area:

 float ballx = GameObject.Find("Ball1").transform.position.x;
 float bally = GameObject.Find("Ball1").transform.position.y;
 float ballz = GameObject.Find("Ball1").transform.position.z;
 
 if (bally > 0.3f && bally < 0f && ballz > 1.3f && ballz > 0.6f && ballx > -1.32f && ballx > -1.7f) {

     Debug.Log("out")
 
 }

I have already tried to create an empty with a Collider and working with OnCollisionExit and OnTriggerExit but i couldn't make it. I know it may not be the best way to do something like this, but i think its the one that i have closer

I have also checked the similar questions

Thank you

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 ForceVII · Nov 25, 2020 at 09:47 PM 0
Share

maybe you should search for OnTriggerEnter. Waay to useful.

avatar image ForceVII · Nov 25, 2020 at 09:48 PM 0
Share

OFF TOPIC also try to avoid using Find(). It slows down your game too much. you can use static instances or drag drop them in the inspector for better performance.

2 Replies

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

Answer by Hellium · Jan 08, 2019 at 12:38 PM

  Vector3 ballPosition = GameObject.Find("Ball1").transform.position ;
  Vector3 minPosition = new Vector3( 0, 0.6f, -1.7f );
  Vector3 maxPosition = new Vector3( 0.3f, 1.3f, -1.32f );
  bool isIn = true;
 
  for( int i = 0 ; i < 3 && isIn ; ++i )
  {
      if( ballPosition[i] < minPosition[i] || ballPosition[i] > maxPosition[i] )
          isIn = false ;
  }
 
  if( isIn )
      Debug.Log( "The ball is inside the area" ) ;
  else
      Debug.Log( "The ball is outside of the area" ) ;


Or, closer solution to yours

  Vector3 ballPosition = GameObject.Find("Ball1").transform.position ;
  Vector3 minPosition = new Vector3( 0, 0.6f, -1.7f );
  Vector3 maxPosition = new Vector3( 0.3f, 1.3f, -1.32f );
 
  if(
       ballPosition.x < minPosition.x || ballPosition.x > maxPosition.x ||
       ballPosition.y < minPosition.y || ballPosition.y > maxPosition.y ||
       ballPosition.z < minPosition.z || ballPosition.z > maxPosition.z
  )
      Debug.Log( "The ball is outside of the area" ) ;
  else
      Debug.Log( "The ball is inside the area" ) ;



Comment
Add comment · Show 1 · 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 mbvila · Jan 08, 2019 at 06:09 PM 0
Share

It worked, thanks a lot!!

avatar image
1

Answer by robertseegrist · May 06, 2021 at 03:32 AM

You should be able to just use Bounds.Contains.

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

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

214 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 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

instantiated gameobject position is not working 0 Answers

Why transform.localPosition is incorrect on mobile? 1 Answer

Make gun aim? 0 Answers

Having trouble moving a GameObject to another scene and Transform it to a different position 0 Answers

After using transform.position, gameobject resets z-axis position 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