Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

compass on GUI rotate with camera!

Discussion in 'Immediate Mode GUI (IMGUI)' started by lcl113456477, Mar 19, 2010.

  1. lcl113456477

    lcl113456477

    Joined:
    Mar 1, 2010
    Posts:
    2
    :cry: i dont know how control to compass rotation when rotate camera!
    please help me!
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class zhinanzhen : MonoBehaviour
    7. {
    8.     public Texture pan;
    9.     private Rect panRect = new Rect(Screen.width - 270,20, 250, 250);
    10.     // Use this for initialization
    11.     float zhizhenrotationX = 0F;
    12.     private float sensitivityX =15F;
    13.     float thisAngle;
    14.     public Transform camera;
    15.     void Start () {
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void OnGUI () {
    21.         Matrix4x4 matrixBackup = GUI.matrix;
    22.         if(Input.GetMouseButton(0)){
    23.            
    24.             //zhizhenrotationX = Input.GetAxisRaw("Mouse X") * sensitivityX;
    25.             //thisAngle+= zhizhenrotationX;
    26.            thisAngle= camera.transform.rotation.y * 360;
    27.             Vector2 pos = new Vector2(Screen.width - 270 + 125, 145);
    28.  
    29.             print( thisAngle);
    30.             GUIUtility.RotateAroundPivot(thisAngle, pos);
    31.            
    32.          
    33.          
    34.         }
    35.         GUI.DrawTexture(panRect, pan);
    36.         GUI.matrix = matrixBackup;
    37.     }
    38. }
    39.  
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    This code doesn't look too bad. Can you explain in more detail what is going wrong?
     
  3. alvarofrutos

    alvarofrutos

    Joined:
    May 24, 2010
    Posts:
    1
    Hi, i did this. I hope it helps:

    Code (csharp):
    1.  
    2.  
    3. GUI.skin = compassGUISkin;
    4.        
    5.         var compassAngle : float = transform.rotation.eulerAngles.y;
    6.         var compassDiameter : float = 128;
    7.         var compassWidth : float = 16;
    8.         var compassWindowRect : Rect = Rect (compassPos.x,compassPos.y, compassDiameter, compassDiameter);
    9.         GUI.Label (compassWindowRect, "");
    10.         var cosAngle : float = Mathf.Cos(compassAngle*Mathf.PI/180);
    11.         var sinAngle : float = Mathf.Sin(compassAngle*Mathf.PI/180);
    12.         var xMove : float = (compassDiameter*sinAngle/2.0 + compassWidth*cosAngle/2.0);
    13.         var yMove : float = (-compassDiameter*cosAngle/2.0 + compassWidth*sinAngle/2.0);
    14.         var compassPos : Vector3 = new Vector3(compassPos.x + compassDiameter/2.0 + xMove,compassPos.y + compassDiameter/2.0  + yMove, 0); //position for matrix
    15.         var compassQuat : Quaternion = Quaternion.identity; //rotation for matrix
    16.         compassQuat.eulerAngles = Vector3(0, 0, compassAngle + 90); //set the rotation to something - rotate around z!
    17.         GUI.matrix = Matrix4x4.TRS(compassPos, compassQuat, Vector3.one); //Apply the matrix
    18.         GUI.Box(Rect(0, 0, compassDiameter, compassWidth), ""); //notice how the rect starts at 0/0 and the matrix handles the position
    19.  
    20.  
    You'll have to add the variables compassGUISkin and compassPos.

    compassPos is a vector2 with the position of the compass

    compassGUISkin is a GUISkin with the textures of the compass, in the Label texture use the compassBackground and in the Box texture use the arrow.

    You must attach this object to the camera or refer the "var compassAngle : float = transform.rotation.eulerAngles.y;" to the camera.