Search Unity

Cursor - Problem with GUI texture rotation

Discussion in 'Scripting' started by Dinrae, Jan 2, 2014.

  1. Dinrae

    Dinrae

    Joined:
    Dec 19, 2012
    Posts:
    29
    Hello,

    Script idea is to use the mouse cursor to show the direction the player is facing.
    What the script is doing is converting mouse position to get fAngle (0° to 360°). Then, fAngle is used to rotate mouse cursor (mAngle).
    When i look at debug log, fAngle and mAngle are correct, but mouse cursor rotation is weird.
    What did i dit wrong?

    Thanks in advance!

    using UnityEngine;
    using System.Collections;

    Code (csharp):
    1. public class MouseCursorAngle : MonoBehaviour {
    2.  
    3.     public GameObject goPlayer;
    4.  
    5.     // Cursor texture
    6.     public Texture2D textCursor;
    7.     // Cursor size x
    8.     public int cursorSizeX = 16;
    9.     // Cursor size y
    10.     public int cursorSizeY = 16;
    11.  
    12.     private float angle= 0.0f;
    13.  
    14.  
    15.     void  Start (){
    16.         //Hide Mouse Cursor
    17.         Screen.showCursor = false;
    18.         }
    19.  
    20.  
    21.     void Update() {
    22.         GetAngle ();
    23.     }
    24.  
    25.  
    26.     public void GetAngle(){
    27.  
    28.         Vector3 v3Pos;
    29.         float fAngle;
    30.  
    31.         //Convert the player to Screen coordinates
    32.         v3Pos = Camera.main.WorldToScreenPoint(goPlayer.transform.position);
    33.         v3Pos = Input.mousePosition - v3Pos;
    34.         fAngle = Mathf.Atan2 (v3Pos.y, v3Pos.x)* Mathf.Rad2Deg;
    35.         if (fAngle < 0.0f) fAngle += 360.0f;
    36.         Debug.Log ("fAngle) "+fAngle);
    37.         angle = fAngle;
    38.         Debug.Log ("mAngle) "+angle);
    39.     }
    40.  
    41.  
    42.     void OnGUI()
    43.     {
    44.         Matrix4x4 matx= GUI.matrix;
    45.         float x= Event.current.mousePosition.x-cursorSizeX/2.0f;
    46.         float y= Event.current.mousePosition.y-cursorSizeY/2.0f;
    47.         Vector2 pivot= new Vector2(x + cursorSizeX / 2.0f,y + cursorSizeY / 2.0f);
    48.         GUIUtility.RotateAroundPivot(angle, pivot);
    49.         GUI.DrawTexture ( new Rect(x, y, cursorSizeX, cursorSizeY), textCursor);
    50.         GUI.matrix = matx;
    51.     }
    52. }