Home > database >  How to get an angle between 3 points?
How to get an angle between 3 points?

Time:12-31

I need to get the angle between the direction ball A is moving and Ball B on the x and z axis. Also i am using Unity Netcode (idk if it makes a difference or not)

enter image description here

so far i have tried to get i with 3 points A-ball A position, B-ball B position, C-ball A position on last frame. i've tried

float angle = Mathf.Atan2(B.y - A.y, B.x - A.x) - Mathf.Atan2(A.y - C.y, A.x - C.x);

and it gave me an angle but it was nowhere close to what it should have been. Then i copied some Acos code off of here but that didn't work ither.

float dot = C.x * B.x   C.y * B.y;
float angle = Mathf.Acos(dot);

then i tried Vector3.Angle and it sometimes worked but sometimes it didn't.

float angle = Vector3.Angle(B, C);

plz help, i'm new to this.

CodePudding user response:

A, B and C are not vectors, they are points. If you want to compute the angle between two vectors, you should use B - A and A - C.

float angle = Vector3.Angle(B - A, A - C);
  • Related