Is it possible to access the gameobject in front of another gameobject?
So, let's say we have a bot called "bot". The bot has to determine whether Cube (1) or Cube (2) is in front of him and then access the script assigned to that cube. Let's say the bot x-axis is 10, while the cube's in front of him 11.
Is this possible to do?
Answer by tormentoarmagedoom · Apr 23, 2018 at 10:38 AM
Good day.
You have a mathematic problem here, but it can be solved, of course!
If you have all the possible cubes to be selected, so you know all their positions, you can make a simple angle test.
If you know ehre the bot is looking at, create a vector with that direction. Lets call this vector "Angle 0"
Then Create a Vector for each cube from the bot to the cube (so you will have one vector for each cube) Lest call them cubeVectors
then simply check the angles betweenthe angle 0 and each cubeVector ( with vector3.angle ). The lower angle you get, is the cube "more in front". You can add a maximum angle to consider is "in front" to be sure is not selected a cube whichj is not in front, but is the "more close to front".
To do this you need to knoe some tools of vetors and angles, all of them exists :D just look for them and learn how to use it. You need to understand what are you doing to suceed!
If helped, accept the answer and close the question! Bye!
Your answer
