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

How would I add an input?

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

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that zooms out the camera when you hold the Sprint input. Here's the script...
    Code (JavaScript):
    1. var zoom : int = 80;          //determines amount of zoom capable. Larger number means further zoomed in
    2. var normal : int = 60;        //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("Sprint") )
    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.  
    Could someone change the input so that it only zooms out when the Input Sprint is held AND my CharacterController is moving forward? Thanks!
     
    Last edited: Feb 17, 2015
  2. MidgardDev

    MidgardDev

    Joined:
    Dec 11, 2012
    Posts:
    47
    First thing to ask is: Are you using a CharacterController or rigidbody?

    If you're using CharacterController you would need to know how to get the current velocity of your controller, I do not use CharacterControllers so I don't know how it's done if it's your case.

    Using rigidbody, it would just be like the following:

    Code (CSharp):
    1.  
    2. if(Input.GetButton("Sprint") && rigidbody.velocity.magnitude > .1f)
    3. {
    4.       // Do something
    5. }
    6.  
    Let me know if it did work!
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    sorry im using a character controller
     
  4. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    If you use the Up arrow for moving forward, then it would look something like :
    Code (csharp):
    1.  
    2. if(Input.GetButton("Sprint") && Input.GetAxis("Horizontal") > 0)
    3. {
    4.     //do something
    5. }