- Home /
How can I check if a specific gameObject is located in a specific "room"?
Hello Unity community, first time poster here. Here's my issue: I am attempting to store, track and compare the location of many gameObjects within a scene and ultimately compare their location against a set of criteria.
Example
A VCR game object is located in room A and the player picks up the VCR and moves it to room B. I would like to know how to compare the location of the VCR from its original position to its final position.
What I've tried (or attempted to try)
I've attempted to use gameobject.transform to get the x,y, and z position and compare that to the final position but that only resulted in me knowing it moved, but not to where (room)
***Ultimately what I'd like to do is to say to the player, get item A from room A and move it to room B. Then I'd like to check to see if they have completed the task.
Any help would be greatly appreciated!
Answer by hexagonius · Jun 18, 2017 at 04:12 AM
I guess your rooms are not arranged like a grid. You can use trigger volumes and react to TriggerEnter to know if the player enters a specific room.
Thanks hexagonius. I am not familiar with trigger volumes, I'll have to research that further. Would it be more beneficial to arrange the rooms in a grid?
It would reduce the checks to two axis and would make them easier to do, but it's not worth it, it will also ruin your game design I guess.
Answer by toddisarockstar · Jun 18, 2017 at 01:03 PM
leepkits answer is very exeptable!
Alternatively you can always see if you're withing a box shape simply with 4 greater than/less than statements. a simple example would be:
if (transform.position.x>5f){ //left limit of room
if (transform.position.x<15f){ //right limit of room
if (transform.position.z>6f){ //back limit of room
if (transform.position.z<20f){ //forward limit of room
print("i am in the room");
}}}}