Converting a Rendered Canvas from Overlay to World Space while facing camera
I'm working on converting a 3D game to VR. I've made some progress, but the UI has been challenging. From my understanding (and use) a canvas rendered in world space is recommended in VR.
Here is a video of what I see after converting the overlay canvas to world space. The two arrow sprites follow the hand correctly, but from some angles, we end up looking at them edge-on like this:
But I want them to always face the player's head flat-on like this:
I feel I have identified the issue, but my programming is a work in progress. I'm wondering if this could be the problem:
public static float Atan2(float y, float x);
Description
Returns the angle in radians whose Tan is y/x.Return value is the angle between the x-axis and a 2D vector starting at zero and terminating at (x,y).
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.SocialPlatforms;
public class UIManager : MonoBehaviour
{
public GameObject WindVectorUI;
public GameObject SailDirectionUI;
public GameObject ApparentWindUI;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
UpdateWindVectorUI();
UpdateSailDirectionUI();
UpdateApparentWindUI();
}
// calculates the angle of the wind vector using Mathf.Atan2() which returns the angile in radians between the x-axis and the vector pointing to (x,y)
void UpdateWindVectorUI()
{
float AngleInRad = Mathf.Atan2(WindManager.instance.CurrentTrueWind.y, WindManager.instance.CurrentTrueWind.x);
WindVectorUI.transform.rotation = Quaternion.Euler(0, 0, AngleInRad * Mathf.Rad2Deg);
}
void UpdateSailDirectionUI()
{
SailDirectionUI.transform.rotation = Quaternion.Euler(0, 0, -BoatManager.Player.Sail.transform.localRotation.eulerAngles.y + 90 - BoatManager.Player.transform.localRotation.eulerAngles.y);
}
void UpdateApparentWindUI()
{
float AngleInRad = Mathf.Atan2(BoatManager.Player.ApparentWind.y, BoatManager.Player.ApparentWind.x);
ApparentWindUI.transform.rotation = Quaternion.Euler(0, 0, AngleInRad * Mathf.Rad2Deg);
}
}