So, the last few hours I have been battling with an odd problem and have so far not been able to find a solution on the forums.
I’m trying to calculate the angle (degree) between a playerCharacter and a targetObject/waypoint. Here’s how I do it:
var targetObj : GameObject;
var playerChar : GameObject;
var targetDir : Vector3;
var angle : float;
var dot : float;
function Start ()
{
playerChar = GameObject.Find("playerCharacter");
}
function Update()
{
targetObj = GameObject.FindWithTag("firstAidWp");
targetDir = targetObj.transform.position - playerChar.transform.position;
targetDir = targetDir.normalized;
dot = Vector3.Dot(targetDir, playerChar.transform.forward);
angle = Mathf.Acos( dot ) * Mathf.Rad2Deg;
print (angle);
}
Now here’s my problem: On great distances, the angle is ~0 when I’m looking directly at my targetObject (which it should be). However! The closer I move to my targetObject the greater the angle gets. When I’m very close to the object and looking directly at it, the angle will be around 20 degree.
Anyone know why this might happen? I’d like the angle to remain the same no matter the distance between the two objects. E.g. ~0 degree when I’m looking straight at it, no matter the distance between the playerCharacter and the targetObject/waypoint.
Any help is very appreciated,
Yarry ![]()