Check if one gameobject in sight of another gameobject give wrong angle.
Hi, I am trying to detect if food object contains to my player object's sight, but angle is wrong by 90. I wrote the same code as in this tutorial:
but when I log the angle is greater by 90:
So here is my code: I don't know what I am doing wrong really, object in the same layer and parent.
var player = players[i];
var oneFood = food[j];
var foodToPlayer = oneFood.transform.position - player.transform.position;
var angle = Vector3.Angle(foodToPlayer, player.transform.forward);
Debug.Log(angle);
Answer by horchynskyi · Jul 19, 2019 at 06:52 PM
I found solution. Just switched x and z position. But I don't know reason of this. So if anyone can explain me, I will be very grateful. new code is:
var foodToPlayer = oneFood.transform.position - player.transform.position;
var playerForward = player.transform.forward;
var angle = Vector3.Angle(foodToPlayer, new Vector3(playerForward.z, playerForward.y, playerForward.x));
Your answer
Follow this Question
Related Questions
Struggling to get the rotation the player is moving in. 1 Answer
Attempting to apply force to object, paralell to mouse drag. 2 Answers
change angle towards direction 1 Answer
Get angle between facing direction and a point 0 Answers
Help Detecting side of an vector 3, How to check direction and face between 2 vector3 0 Answers