- Home /
Vector3.Angle() Never Reaching 0
Hello, Im trying to calculate angle by Vector3.Angle(). I drew the result in a professional graphic editor:
It never reach 0, stops at 10, why? My code:
Vector3 targetDir = Player.transform.position - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
Debug.Log(angle);
Answer by Bunny83 · Apr 26, 2018 at 05:07 PM
Well, we have no idea how you actually align your forward direction with the direction to your enemy. Keep in mind that your drawing is 2d while in reality your player object and the target object have 3d coordinates. So if this is a 2d game you may have located your player and target object at different z values? Specifically a difference of about "0.173" times the distance between your player and target (assuming it's actualy 10°). If not you have to add more details on what you do to align your player to the target.
Oh right, I totally forgot about height, it may cause problem, It's 3d project. How can I calculate angle no matter how high is the target? Angle between car and target to know how to turn the wheels.
If you want to ignore say the z position / component, just set it to 0 in both vectors:
Vector3 targetDir = Player.transform.position - transform.position;
Vector3 forward = transform.forward;
targetDir.z = forward.z = 0;
float angle = Vector3.Angle(targetDir, forward);
Debug.Log(angle);
This would only consider the x and y components. Though if it's a top down view what you've drawn and you want to ignore y, just do
targetDir.y = forward.y = 0;
This basically projects both direction vectors onto the x-z-plane
Your answer
Follow this Question
Related Questions
Angle to Direction 1 Answer
Find angle to target (not angle to rotate by) 2 Answers
Angle Between vectors in Navigation Mesh 1 Answer
How to create a view angle where the turret can see the enemy and start shooting 1 Answer
Lerp problem 1 Answer