Search Unity

GetAxisRaw + Double Tap

Discussion in 'Scripting' started by Corvec, Aug 24, 2016.

  1. Corvec

    Corvec

    Joined:
    Aug 24, 2016
    Posts:
    2
    Hi everyone I'm new to unity and i'm trying to understand how I can implement a sprint feature in my script. I have a basic walking script using GetAxis and I would like to understand how I would accomplish registering a double tap input button. I've experimented with a few code snippets but none of have seem to work for me however I also feel I am not understanding the flow of operation and I believe thats where I'm messing up. If someone could look at my code and point me in the right direction or help me to understand why my code has not been working.
    Currently when I execute my code the debug shows both Walking and Running have been activated when I move, if I continue to hold my movement button I then default to Walking only

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Script : MonoBehaviour
    4. {
    5.     public float speed = 6.0f;
    6.     public float jumpSpeed = 8.0f;
    7.     public float gravity = 20.0f;
    8.     public float lastTapTime = 0;
    9.     public float tapSpeed = 0.5f;
    10.     private Vector3 moveDirection = Vector3.zero;
    11.  
    12.     void Start()
    13.     {
    14.         lastTapTime = 0;
    15.     }
    16.     void Update()
    17.     {
    18.        
    19.         CharacterController controller = GetComponent<CharacterController>();
    20.  
    21.         if (controller.isGrounded)
    22.         {
    23.  
    24.             moveDirection = new Vector3(-Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
    25.             if (Input.GetAxis("Vertical")* Time.deltaTime != 0 || Input.GetAxis("Horizontal")* Time.deltaTime != 0)
    26.             {
    27.                 Debug.Log("Walking");
    28.                 if ((Time.time - lastTapTime) < tapSpeed)
    29.                 {
    30.                     Debug.Log("Running");
    31.                 }
    32.                
    33.             }
    34.             else
    35.             {
    36.                 Debug.Log(("Not Walking");
    37.                 lastTapTime = Time.time;
    38.             }
    39.            
    40.             moveDirection = transform.TransformDirection(moveDirection);
    41.          
    42.          
    43.            
    44.             moveDirection *= speed;
    45.  
    46.             if (Input.GetButton("Jump"))
    47.             {
    48.                 moveDirection.y = jumpSpeed;
    49.                 Debug.Log("Jump");
    50.              
    51.             }
    52. }
    53.         moveDirection.y -= gravity * Time.deltaTime;
    54.        
    55.         controller.Move(moveDirection * Time.deltaTime);
    56.        
    57.     }
    58. }
    59.  
    60.      
    61.        
    62.    
    63.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're along the right lines, but I suspect you're being scuppered by the fact that even a tap on an axis is going to register across multiple frames and there isn't a comparable function to GetButtonDown for axis ... if two consecutive frames register the axis being not 0 you'll go straight into "running".

    You might be able to simulate a "GetAxisDown" by storing the input from the last frame (i.e. store the value in a variable at the end of update) and checking to see if there wasn't any last frame (at the start of update).
     
    Corvec likes this.
  3. Corvec

    Corvec

    Joined:
    Aug 24, 2016
    Posts:
    2
    Apologies, while I thought I understood the solution you provided I seem to be stuck once again in the actual implementation. Would you be able to provide a psuedocode example? Right now I'm storing the value of moveDirection at end of update and im trying to understand how I use this information to parse a double tap
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Update the tapAmount value (or something like that) when the current frame's axis value is greater than the last frame's axis value (you'll need to store it to tell). When the axis ceases to be a greater value, check the lastTapTime value and see if the new "tap" is within the timeframe needed to generate a double-tap. After that, register the current time as the lastTapTime, whether the double-tap was successful or not, and reset the tapAmount to zero. You'll probably need to create dead-zone logic as well, so that gravity and barely touching the controller don't generate tap records, and also a minimum amount (and maximum time per motion) the axis has to move for it to be considered a tap, even if it's beyond the dead-zone value.

    That should give you some ideas anyways- I'm not an expert in this area.
     
    Corvec likes this.