- Home /
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 :) 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
Rick
I'm sorry, but I don't understand a lot of what you've written here. Can you please explain what, for instance, the following line means:
I need the results of the angle in 360° degrees but have +180° 0° then back on the same side as the compass indicator
?
Answer by aldonaletto · Jan 20, 2013 at 08:41 PM
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).
Nice answer!
BTW: This will return -180 to 180. To get 0 to 360, just add 180 to the result.
That Worked all most, but it does not take account of player rotation. If I look at the target. all is good if I by pass the target all is good. but If the player rotates by 180°, It is not reflected if the player locally rotates. So how do we fix that.
I wish I had Aldo's brain at my screen :)
To which object this script is attached? If you're using the First Person Controller, the script must be attached directly to it (not to the camera!). This code relies on the player being completely upright - if it may tilt, kill the Y component in the forward vector as well:
Vector3 targetDir = target.position - transform.position;
Vector3 forward = transform.forward;
targetDir.y = 0; // Remove any height difference
forward.y = 0;
...