- Home /
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; }
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.
Thanks, i will change the name, shame there isn't a better way of doing it tho lol