It is most propably easy one, but I try to figure this out for a long time (I thought I manage to resolve this, but time ang again it is proven I`m wrong). I manage to get right angle between x and z vector to target, but no luck between y and z ( I thought it will be analogically similar and propably it is)
Here is working code:
Vector2 frontAheadVector2;
Vector2 targetHeadingVector2;
Vector3 targetDirection = Target.transform.position - transform.position;
frontAheadVector2 = new Vector2(transform.forward.x,transform.forward.z);
targetHeadingVector2 = new Vector2(targetDirection.x,targetDirection.z).normalized;
float angleToTargetHor = Vector2.Angle(frontAheadVector2,targetHeadingVector2);
Vector3 cross = Vector3.Cross(frontAheadVector2, targetHeadingVector2);
if (cross.z > 0)
angleToTargetHor *= -1;
And this works good, but here is another one, wich always gives me wrong result:
Vector2 frontAheadVector2;
Vector2 targetHeadingVector2;
Vector3 targetDirection = Target.transform.position - transform.position;
frontAheadVector2 = new Vector2(transform.forward.y,transform.forward.z).normalized;
targetHeadingVector2 = new Vector2(targetDirection.y,targetDirection.z).normalized;
float angleToTargetVer = Vector2.Angle(frontAheadVector2,targetHeadingVector2);
Vector3 cross = Vector3.Cross(frontAheadVector2, targetHeadingVector2);
if (cross.z < 0)
angleToTargetVer *= -1;
I do not understand why it isnt working, on first axis is Y, so understanbly calculations are on x and z axis, and it works, in second I need second axis - X, so angle is calculated from y and z, but it doesnt work. Any solutions?