Search Unity

Need help with crosshair GUI c#

Discussion in 'Scripting' started by traderain, Oct 25, 2014.

  1. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    I don't know what i did wrong but when i press the gui button the crosshair is not showing.
    Any help is apriciated
    Here is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RayTest : MonoBehaviour {
    5.     public Transform cubie;
    6.     public float raycastDist = 100f;
    7.     public Camera myCamera;
    8.     public bool waslocked = false;
    9.     public Texture2D crosshairImage;
    10.    
    11.     void OnGUI () {
    12.         if(GUI.Button(new Rect(20,40,80,20), "Lock cursor")) {
    13.             waslocked = true;
    14.             Screen.lockCursor = true;
    15.             DrawCrosshair();
    16.         }
    17.     }
    18.        
    19.     // Use this for initialization
    20.     void Start () {
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         RaycastHit hit;
    27.         if(Input.GetKey("mouse 0")) {
    28.             if(  Physics.Raycast(myCamera.transform.position, myCamera.transform.forward, out hit, raycastDist)){
    29.             Debug.Log("Successfull raycast");
    30.                 cubie.position = hit.point;
    31.                 cubie.rotation = Quaternion.LookRotation(hit.normal);
    32.             }
    33.             else
    34.             {
    35.                 Debug.Log ("Not succesfull raycast");
    36.         }
    37.     }
    38.   }
    39.     void DrawCrosshair() {
    40.         float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
    41.         float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
    42.         GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
    43.     }
    44. }
    Thanks for reading.
     
  2. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    Why not create a GUITexture and show and hide it? What you should be doing is putting the drawing into the ONGUI function. Try something like this


    bool drawit;
    void DrawCrosshair (){

    drawit = !drawit;

    }

    void OnGUI (){

    if (drawit){
    //draw stuff here
    }

    }

    Hope this helps! :3
     
  3. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Code (CSharp):
    1.  void OnGUI () {
    2.         if(GUI.Button(new Rect(20,40,80,20), "Lock cursor")) {
    3.             waslocked = true;
    4.             Screen.lockCursor = true;
    5.             DrawCrosshair(); // This only gets called just after the button is clicked.
    6.         }
    7.     }
    For one thing, you're only calling the DrawCrosshair() method for the single frame after the button is released.

    Code (CSharp):
    1. void OnGUI () {
    2.         if(GUI.Button(new Rect(20,40,80,20), "Lock cursor")) {
    3.             waslocked = true;
    4.             Screen.lockCursor = true;
    5.         }
    6.         DrawCrosshair();
    7.     }
    This works just a tad better.