• 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 finlay_morrison · Jan 05, 2019 at 09:33 AM · vector3inside

Checking if a vector3 is within an area defined by 2 other vector 3s

So I have a game where there is an area where you can build your vehicle, I have defined the area with 2 vector3s, in opposite corners of the area that you can build in. I want to check if the vector3 that I pass in is inside this area, I have code that does this but I feel that the code is ugly and I know that there must be a more visually pleasing way of doing the code for this. Here is the code:

private bool CheckIfGhostPartIsInAValidPosition(Vector3 _positionToCheck) { if (_positionToCheck.x < minBuildArea.x || _positionToCheck.x > maxBuildArea.x) { return false; } if (_positionToCheck.y < minBuildArea.y || _positionToCheck.y > maxBuildArea.y) { return false; } if (_positionToCheck.z < minBuildArea.z || _positionToCheck.z > maxBuildArea.z) { return false; } return true; }

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
1

Answer by Bunny83 · Jan 05, 2019 at 10:07 AM

No, there isn't a "more visually pleasing way". Well Unity has already this kind of functionality with the Bounds struct and its Contains method. However internally it will do pretty much the same thing you did.


An improvement would be to rename your method. Such long and convoluted names don't really help with "visually pleasing" code ^^. A name like "InsideBuildingArea" would be descriptive enough. You included "GhostPart" in the method name even the method has nothing to do with a "GhostPart". You can pass it any position which is checked, no matter where it comes from.

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 finlay_morrison · Jan 05, 2019 at 02:51 PM 0
Share

Thanks, i will change the name, shame there isn't a better way of doing it tho lol

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

121 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

Related Questions

Project a position onto a plane whose normal isn't at the origin. 2 Answers

using vector3 lerp causes player to fall through ground 0 Answers

How to make a ragdoll is kinematic entirely. 1 Answer

display to GUI text distance between 2 objects 0 Answers

Bullet Moves To Fast To Detect? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges