Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GUI show only while mouse is held down

Discussion in 'Scripting' started by AtomicCabbage33, Nov 16, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    This script controls the cameras FOV when "Fire2" is pressed as well as displaying a GUI texture. However, once the user presses the button once, the texture displays and does not disappear until the button is pressed again - I want it to only show when the button is held down?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class awpScopeIn : MonoBehaviour {
    5.  
    6.     public Texture scopeGUI;
    7.     private bool _isScoped = false;
    8.     public Color color = Color.black;
    9.     private Camera cam;
    10.     public GameObject awp_graphics;
    11.  
    12.     void Start()
    13.     {
    14.         cam = GetComponentInParent( typeof(Camera) ) as Camera;
    15.         cam.clearFlags = CameraClearFlags.SolidColor;
    16.         cam.fieldOfView = 70f;
    17.     }
    18.    
    19.     void OnGUI()
    20.     {
    21.         float width = 600;
    22.         float height = 600;
    23.        
    24.         if (_isScoped) {
    25.             GUI.DrawTexture (new Rect ((Screen.width / 2) - (width/2), (Screen.height / 2) - (height/2), width, height), scopeGUI);  
    26.         }
    27.     }
    28.    
    29.     void Update()
    30.     {
    31.         if(Input.GetButtonDown("Fire2"))
    32.         {
    33.             _isScoped = !_isScoped;
    34.             cam.backgroundColor = color;
    35.             cam.fieldOfView = 45f;
    36.             awp_graphics.gameObject.SetActive(false);
    37.         }else if(Input.GetButtonUp("Fire2"))
    38.         {
    39.             cam.fieldOfView = 70f;
    40.             awp_graphics.gameObject.SetActive(true);
    41.         }
    42.    
    43.     }
    44. }
    45.  
     
  2. Zee_pso

    Zee_pso

    Joined:
    Nov 12, 2016
    Posts:
    31
    You forgot to set isScoped to false once getButtonUp is called. Right now, you have isScoped being toggled on button down, so it toggles once the button is pressed again.
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Oh right yeah, my bad thanks