Howdy all. Been having a pretty good time with Unity so far, but I ran into an issue I just can’t seem to google my way out of so I figured I would post here.
I am trying to make a radar/minimap for a flight game that displays the location of enemies relative to the player. So in OnGUI in the radar script (which is attached to the player), I use GUIUtility.RotateAroundPivot() to rotate the radar using transform.eulerAngles.y. This basically works, but the radar jitters around slightly. I managed to replicate this issue in a simplified setting. I will post the code here.
Movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testmovement : MonoBehaviour
{
// Update is called once per frame
void Update()
{
transform.Rotate(0f, 300f * Time.deltaTime, 0f);
}
}
Radar script:
using UnityEngine;
using System.Collections;
public class testGUI : MonoBehaviour
{
public Texture2D texture;
float size = 150f;
Vector2 offset;
Rect rectangle;
Vector2 pivotPoint;
// Start is called before the first frame update
void Start()
{
offset = new Vector2 (Screen.width - size, Screen.height - size);
rectangle = new Rect(offset.x, offset.y, size, size);
pivotPoint = offset + new Vector2(size / 2f, size / 2f);
}
// Update is called once per frame
void OnGUI()
{
GUIUtility.RotateAroundPivot(-transform.eulerAngles.y, pivotPoint);
GUI.DrawTexture(rectangle, texture);
}
}
If one attaches these two scripts to the same object and then provides a texture to the radar script, that would be sufficient to replicate the issue. I assume it’s happening because Update() runs out-of-sync with OnGUI(), though I don’t completely get why that would cause jitter. LateUpdate() and FixedUpdate() don’t fare any better, naturally. If anyone has any insight, I would be terribly grateful. Maybe this just isn’t an appropriate use for GUIUtility.RotateAroundPivot()? Should I apply the textures to 2D planes and then rotate those in Update() instead to try to get things to sync up? I would prefer not to have to do that, as it seems kind of tricky, but maybe there’s no choice.