Compass display 0 to +180 instead of 360°around.

I have to deal with the feared 180°-0° and back problem. I need the results of the angle in 360° degrees but have +180° 0° then back on the same side as the compass indicator :slight_smile: makes it hard to find the target that way-lol

Also I noticed that the target indicator stays to only one side of the compass.

0° on the compass is 180° in the script, 180° on the compass is 0° in the script

All on the left side (180° to 360°) of the compass and never the full 0° to 360° around the compass perimeter.

using UnityEngine;
using System.Collections;

public class Target_Indicator : MonoBehaviour
    public Transform target; //Object that will be set in the Inspector.
    public Transform indicatorPlane; // Drag the indicator here
	public float angle = 0; //Create sting, Now we can see it in the inspector.
	void Update()
		Vector3 targetDir = target.position - transform.position;
     	Vector3 forward = transform.forward;
     	targetDir.y = 0; // Remove any height difference
     	angle = Vector3.Angle(targetDir, forward);
     	indicatorPlane.eulerAngles = new Vector3(0, angle, 0);
		void OnGUI()		
			float displayValue = Mathf.Round(angle);//Change decimal to hole numbers.
			//Make a GUI label - position on sccreen - add "angle:" - display the value.
			GUI.Label(new Rect(30, 475, 150, 50), "angle: " + displayValue.ToString());

I’ve seen other posts but the use a different structure than mine.
One noted how to change to -180° 0° +180°, but again, I need 360°

It would stand to reason that when one works with rotation, the program whould display
0° to 360° or a option the of.

C# helllllp


That’s because Vector3.Angle always returns an unsigned value - you can’t know whether it’s positive or negative. You could use Vector3.Cross to find the angle polarity: the cross product results in a vector perpendicular to the two ones passed as arguments; considering that these vectors are perpendicular to world Y, you will have a positive Y value when the second vector is to the right, and negative when it’s to the left (or the opposite - everybody gets confused about who’s who in the cross product):

    angle = Vector3.Angle(targetDir, forward);
    angle *= Mathf.Sign(Vector3.Cross(forward, targetDir).y);
    indicatorPlane.eulerAngles = new Vector3(0, angle, 0);

This imposes the sign of the cross product’s Y coordinate to the angle. If the sign is opposed to what you expect, swap the cross arguments (change to targetDir,forward).