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. Dismiss Notice

Make script not work when input is held?

Discussion in 'Scripting' started by Treasureman, Feb 19, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that makes your camera zoom in when the input 'Aim' is held. Here's the code...
    Code (JavaScript):
    1. var zoom : int = 19;          //determines amount of zoom capable. Larger number means further zoomed in
    2. var normal : int = 40;        //determines the default view of the camera when not zoomed in
    3. var smooth : float = 5;       //smooth determines speed of transition between zoomed in and default state
    4. private var zoomedIn = false; //boolean that determines whether we are in zoomed in state or not
    5.  
    6. //This function toggles zoom capabilities with the Z key. If it's zoomed in, it will zoom out
    7. function Update()
    8. {
    9.         zoomedIn = false;
    10.     if( Input.GetButton("Aim") )
    11.     {    
    12.         zoomedIn = true;
    13.     }
    14.    
    15.     //If "zoomedIn" is true, then it will not zoom in, but if it's false (not zoomed in) then it will zoom in.    
    16.     if( zoomedIn == true )
    17.     {  
    18.         camera.fieldOfView = Mathf.Lerp( camera.fieldOfView, zoom, Time.deltaTime*smooth );
    19.     }
    20.     else
    21.     {
    22.         camera.fieldOfView = Mathf.Lerp( camera.fieldOfView, normal, Time.deltaTime*smooth );
    23.     }
    24. }
    25.  
    How would I make it not work if you hold in this input?
    Code (JavaScript):
    1. if(Input.GetButton("Sprint") && Input.GetAxis("Vertical") > 0)
    2.     {    
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1. if (Input.GetButton ("Aim") && !(Input.GetButton ("Sprint") && Input.GetAxis ("Vertical") > 0))
    2. {
    3.     zoomedIn = true;
    4. }
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    that's not what I was looking for. I meant that if you hold in the input I added then it will stop the script from working.
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Ok, then in a separate script...
    Code (csharp):
    1. var zoomScript : ZoomScript;
    2.  
    3. function Update () {
    4.     if (Input.GetButton ("Sprint") && Input.GetAxis ("Vertical") > 0) {
    5.         zoomScript.enabled = false;
    6.     } else {
    7.         zoomScript.enabled = true;
    8.     }
    9. }
    But then you wouldn't be able to take advantage of the lerping like you would with my other suggestion.
     
  5. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I added the script and I got this error...
    Assets/AimDisable.js(1,18): BCE0018: The name 'ZoomScript' does not denote a valid type ('not found').
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    That was a placeholder name because I don't know what the name of your script is.