Can't get correct angle between two world positions
I want to get the angle from transform.position
to TargetPos
, and apply it to transform.eulerAngles.y
.
I can calculate a target position and apply it to the line renderer, which draws the line perfectly:
TargetPos = StartPos + new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f)) * _dist;
lineRend.SetPosition(0, transform.position);
lineRend.SetPosition(1, TargetPos);
If I try to get the angle, it just returns a small, one-digit value.
// Set direction
float TargetDir = Vector3.Angle(transform.position, TargetPos);
// Set rotation
Vector3 rot = transform.eulerAngles;
rot.y = TargetDir;
transform.eulerAngles = rot;
How can I make this work? Thanks!
Comment
Best Answer
Answer by matharoo · Aug 02, 2019 at 01:45 PM
Fixed by using Vector2 instead:
Vector2 vecTarget = new Vector2(TargetPos.x, TargetPos.z);
Vector2 vecCurrent = new Vector2(transform.position.x, transform.position.z);
float TargetDir = Vector2.SignedAngle(vecTarget - vecCurrent, new Vector2(0, 1));
Your answer
Follow this Question
Related Questions
Rotate GameObject 90 degrees where is my flaw? 0 Answers
change angle towards direction 1 Answer
How to convert User Values into degree(Angle)?? 2 Answers
Rotating 3d person character 0 Answers
Struggling to get the rotation the player is moving in. 1 Answer