'3D Compass in Hololens

I'm trying to develop a simple 3D Compass for Hololens 2, using Unity, but my sprites aren't aligned with the Cubes.

I wanted it to be like this: https://www.youtube.com/watch?v=3RuOq9ldX9g

If I move the cubes, the compass works as intended, but it still doesn't align with my FOV.

My code for getting the position on the compass is the following:

    Vector2 GetPosOnCompass(QuestMarker marker)
{
    Vector2 playerPos = new Vector2(Player.transform.position.x, Player.transform.position.z);
    Vector2 playerFwd = new Vector2(Player.transform.forward.x, Player.transform.forward.z);

    float angle = Vector2.SignedAngle(marker.position - playerPos, playerFwd);

    return new Vector2(compassUnit * angle, 0f);
}

Best regards,

Carlos

This is the full Code:

using UnityEngine.UI;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Compass : MonoBehaviour
{
    public GameObject iconPrefab;
    List<QuestMarker> questMarkers = new List<QuestMarker>();

    public RawImage CompassImage;
    public Transform Player;
    public Text CompassDirectionText;

    float compassUnit;

    public QuestMarker one;
    public QuestMarker two;
    public QuestMarker three;

    private void Start()
    {
        compassUnit = CompassImage.rectTransform.rect.width / 360f;

        AddQuestMarker(one);
        AddQuestMarker(two);
        AddQuestMarker(three);
        
    }

    public void Update()
    {
        foreach (QuestMarker marker in questMarkers)
        {
            marker.image.rectTransform.anchoredPosition = GetPosOnCompass(marker);

        }


        //Get a handle on the Image's uvRect
        CompassImage.uvRect = new Rect(Player.localEulerAngles.y / 360, 0, 1, 1);

        // Get a copy of your forward vector
        Vector3 forward = Player.transform.forward;

        // Zero out the y component of your forward vector to only get the direction in the X,Z plane
        forward.y = 0;

        //Clamp our angles to only 5 degree increments
        float headingAngle = Quaternion.LookRotation(forward).eulerAngles.y;
        headingAngle = 5 * (Mathf.RoundToInt(headingAngle / 5.0f));

        //Convert float to int for switch
        int displayangle;
        displayangle = Mathf.RoundToInt(headingAngle);

        //Set the text of Compass Degree Text to the clamped value, but change it to the letter if it is a True direction
        switch (displayangle)
        {
        case 0:
            //Do this
            CompassDirectionText.text = "N";
            break;
        case 360:
            //Do this
            CompassDirectionText.text = "N";
            break;
        case 45:
            //Do this
            CompassDirectionText.text = "NE";
            break;
        case 90:
            //Do this
            CompassDirectionText.text = "E";
            break;
        case 130:
            //Do this
            CompassDirectionText.text = "SE";
            break;
        case 180:
            //Do this
            CompassDirectionText.text = "S";
            break;
        case 225:
            //Do this
            CompassDirectionText.text = "SW";
            break;
        case 270:
            //Do this
            CompassDirectionText.text = "W";
            break;
        default:
            CompassDirectionText.text = headingAngle.ToString ();
            break;
        }
    }

    public void AddQuestMarker(QuestMarker marker)
    {
        GameObject newMarker = Instantiate(iconPrefab, CompassImage.transform);
        marker.image = newMarker.GetComponent<Image>();
        marker.image.sprite = marker.icon;

        questMarkers.Add(marker);
    }

    Vector2 GetPosOnCompass(QuestMarker marker)
    {
        Vector2 playerPos = new Vector2(Player.transform.position.x, Player.transform.position.z);
        Vector2 playerFwd = new Vector2(Player.transform.forward.x, Player.transform.forward.z);

        float angle = Vector2.SignedAngle(marker.position - playerPos, playerFwd);

        return new Vector2(compassUnit * angle, 0f);
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source