Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

c# - Always point to north compass

I am able to make Compass through implementing a tutorial here. Here is the code which is working perfectly.

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
}

I want to change bliptex with arrow but its not working correctly. loosing the center of the compass image. enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to set size of rect in

GUI.DrawTexture(new Rect(0, 0, 120, 120), compBg);

and set center position accordingly in

blipX += 55;
blipY += 55;

I assume 120 and 55 are values based size of the texture he is using, as you are using a different texture you need to set size of the texture appropriately.

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...