Я могу сделать Compass через реализацию tutorial here. Вот код, который работает отлично.Всегда указывайте на север компас
using UnityEngine;
using System.Collections;
/// <summary>
/// 2d GUI Compass
/// </summary>
public class CompassWithDirectionMovement : MonoBehaviour
{
#region Vars
public Transform player;
public Texture compBg;
public Texture blipTex;
#endregion Vars
#region UnityEvents
void OnGUI()
{
//GUI.DrawTexture(new Rect(0, 0, 120, 120), compBg);
//GUI.DrawTexture(CreateBlip(), blipTex);
GUI.DrawTexture(new Rect(0, 0, 120, 120), compBg);
GUI.DrawTexture(CreateBlip(), blipTex);
}
#endregion UnityEvents
#region CustomMethods
Rect CreateBlip()
{
//calculating player's angle
Debug.Log("Player LocalEulerY" + player.eulerAngles.y);
float angDeg = player.eulerAngles.y - 90;//bliper goes on Top
float angRed = angDeg * Mathf.Deg2Rad;//Convert degree to radian;
Debug.Log("Mathf.Cos(angRed) : "+ Mathf.Cos(angRed));
Debug.Log("Mathf.Sin(angRed) : "+ Mathf.Sin(angRed));
//calclulate blipper postion
float blipX = 25 * Mathf.Cos(angRed);//25 pixel from center
float blipY = 25 * Mathf.Sin(angRed);
Debug.Log("blipX : "+ blipX);
Debug.Log("blipY : "+ blipY);
blipX += 55;//half of the size of the background of compass which is center and slight minus it
blipY += 55;//
Debug.Log("blipX : "+ blipX);
Debug.Log("blipY : "+ blipY);
return new Rect(blipX, blipY, 10, 10);
}
#endregion CustomMethods
}
Я хочу изменить bliptex со стрелкой, но не работает правильно. теряя центр изображения компаса.
И где код, который не работает отлично? – TheLethalCoder
Я бы посоветовал использовать UI-систему Unity вместо 'OnGUI()' (этот метод следует использовать для отладки только потому, что он действительно тяжелый для запуска (называемый несколько раз за кадр)). Также, делая это с Unity UI, вам просто нужно разместить где-нибудь свой фон, установить blipper как дочерний и, наконец, повлиять на значение blipper 'RectTransform.localEulerAngle.z' :) – Kardux
В любом случае вам может потребоваться избавиться от магические числа. Создайте некоторые постоянные переменные, такие как centerX и centerY, и используйте их. – Aziuth