unity calculate angle between two vectors code example
Example 1: get angle between two vectors unity
Vector3 targetDir = target.position - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
Example 2: check the angle between two game objects unity
Vector3 dir = GameObject1.position - GameObject2.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Example 3: how to offset vector 3 by an angle unity
//Setting up Vector3's for rays Vector3 rayPosition = new Vector3(transform.position.x, headHeight, transform.position.z); Vector3 leftRayRotation = Quaternion.AngleAxis(-fovAngle, transform.up) * transform.forward; Vector3 rightRayRotation = Quaternion.AngleAxis(fovAngle, transform.up) * transform.forward; //Constructing rays Ray rayCenter = new Ray(rayPosition, transform.forward); Ray rayLeft = new Ray(rayPosition, leftRayRotation); Ray rayRight = new Ray(rayPosition, rightRayRotation); //(Doing things with rays...)