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

[noob] Input.GetKeyDown("Jump") and other input problems...

Discussion in 'Scripting' started by BellokTV, Dec 17, 2018.

  1. BellokTV

    BellokTV

    Joined:
    Dec 14, 2018
    Posts:
    17
    Ok... so the issue that I am having seems to be completely random. I have a 2d platformer that I have been working on and heard in some tutorials that referencing input Axes names from the inputManager would return the appropriate keys and joystick inputs assigned to those Axes.

    I want to use Axes input names for my controls... I don't want to have to assign each individual key in my script... but the more I try... the more inputs randomly break on me... it makes no sense.

    "Horizontal" worked without a problem.
    "Jump" would not work at all. Input Key named: Jump is unknown. I have been forced to assign the space key in my script.
    "Crouch" worked for a while, until I tried using a variable named isCrouched as something to be considered before jumping. After all, if someone is crouching in a platformer, then - logically - pressing jump should cause them to fall off of a platform. I wouldn't want the script to think that they should jump up from a crouched position.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed; // how fast our hero can move. set in the Unity Inspector
    8.     private float moveInput; // a value to check for move direction and key input
    9.     private Rigidbody2D rb; // this lets us with collision, the player sprite must have a rigid body component in order for this to matter
    10.     private bool facingRight = true; // we use this to flip the player sprite when moving left
    11.     private bool isGrounded; // this is a value to check whether or not the player sprite is on the ground or platform... is dependant on how we write the code
    12.     public Transform groundCheck; // helps with the isGrounded check
    13.     public float checkRadius; // how large of an area needs to be checked for groundCheck?
    14.     public LayerMask whatIsGround; // this lets us set a layer in Unity as the ground. All walkable platforms must be on this layer or the player sprite will be unable to jump
    15.     public float jumpForce; // a customisable value for how strong a jump will be from the ground
    16.     //public float jumpForceValue; // This value should be the same as jumpForce while on the ground and be reduced by each extra jump
    17.     private int extraJumps; // a value we can set in the Unity Inspector to allow double, triple, or even higher number of jumps.
    18.     public int extraJumpsValue; // we will use this to reduce the number of jumps available after each extra jump so we can't jump forever
    19.     public Animator anim; // lets grab something from the animator in unity and call it with the anim identifier
    20.     public bool isCrouched; //variable to check if we are crouched
    21.  
    22.     void Start(){
    23.         anim = GetComponent<Animator>();
    24.         extraJumps = extraJumpsValue; //reset the number of jumps available to default.
    25.         //jumpForceValue = jumpForce; // reset the force of our jump to its default
    26.         rb = GetComponent<Rigidbody2D>(); // lets identify the player as 2D physics body so that it doesn't fall through anything set as a solid.
    27.     }
    28.  
    29.     void FixedUpdate(){
    30.  
    31.         //player movement related
    32.         moveInput = Input.GetAxis("Horizontal"); // checks for keyboard input as recognized by Unity and which direction Unity says we should move.
    33.  
    34.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround); // checks to see if our groundcheck object has collided with the ground layer
    35.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y); // checks to see how fast we should move
    36.  
    37.         //animator variables
    38.         anim.SetFloat("Speed", Mathf.Abs(moveInput)); //animator needs to know if we are moving, but it can't give us a run animation with negative numbers, so we use an absolute value derived from moveInput.
    39.         anim.SetBool("Grounded", isGrounded); //animator needs to know if we are grounded or not... aka did we jump? We can grab that from the isGrounded variable
    40.         anim.SetBool("Crouch", isCrouched); //animator needs to know if we are crouched or not... We can grab that from the isCrouched variable
    41.  
    42.         //player status in regards to movement
    43.         if (facingRight == false && moveInput > 0) // if we are facing left but moving to the right,
    44.             {
    45.                 Flip(); // flip the sprite so we look left while moving left
    46.             }
    47.         else if (facingRight == true && moveInput < 0) // otherwise we are facing right but moving left,
    48.             {
    49.                 Flip(); // flip the sprite so we look right while moving right
    50.             }
    51.     }
    52.  
    53.     void Update()
    54.     {
    55.      
    56.         //jumping controller
    57.      if ((isGrounded == true) && !isCrouched)// We are on the ground, but not crouched so... prepare to jump?
    58.         {
    59.             extraJumps = extraJumpsValue; // resets extraJumps to the default value set in the Unity Inspector
    60.             //jumpForceValue = jumpForce; // is supposed to reset jumForceValue to the default value of jumpForce
    61.         }  
    62.      if ((Input.GetKeyDown("space")) && extraJumps > 0) // wait for the space key to be pressed and only allow jumping if extraJumps is greater than 0
    63.         {
    64.                
    65.             rb.velocity = Vector2.up * jumpForce; // perform the jump
    66.             extraJumps--; // reduce the number of jumps allowed while in the air
    67.             //rb.velocity = Vector2.up * jumpForceValue; // perform the jump
    68.             //jumpForceValue--; // reduce the force of jumps made after the first jump
    69.         }
    70.         else if ((Input.GetKeyDown("space")) && extraJumps == 0 && isGrounded == true) // did we touch ground? If we did, reset the number of jumps allowed
    71.         {
    72.             rb.velocity = Vector2.up * jumpForce; // and reset the jumpForceValue because we are back on the ground.
    73.             //rb.velocity = Vector2.up * jumpForceValue; // and reset the jumpForceValue because we are back on the ground.
    74.         }
    75.  
    76.         if (Input.GetButtonDown("Crouch"))
    77.         {
    78.             isCrouched = true;
    79.         }
    80.         else if (Input.GetButtonUp("Crouch"))
    81.         {
    82.             isCrouched = false;
    83.         }
    84.     }
    85.  
    86.     void Flip() // this performs the flip action when required so that we face the right direction while moving
    87.     {
    88.         facingRight = !facingRight;
    89.         Vector3 Scaler = transform.localScale;
    90.         Scaler.x *= -1;
    91.         transform.localScale = Scaler;
    92.     }
    93. }
    94.  
    Now, I realize that the code is a bit messy... I should probably make Jumping its own script... I am new to coding and I tried my best to document it for easier understanding... and I am sure there are better ways of doing some of those things... but... I was following several tutorials for different things...

    Anyway... can anyone help me fix this issue I am having and get back to using (Input.GetKeyDown("Jump"), (Input.GetKeyDown("Crouch"), (Input.GetKeyDown("Fire1"), and such?
     
  2. BellokTV

    BellokTV

    Joined:
    Dec 14, 2018
    Posts:
    17
    Please note that I never got jumpForceValue to work... rather than delete all of it, I commented it out of the code until I learned enough to fix it. It was supposed to reduce the strength of each jump after launching off of the ground...

    That is another issue entirely and not the purpose of this thread.
     
  3. BellokTV

    BellokTV

    Joined:
    Dec 14, 2018
    Posts:
    17
    Oh, it might help to provide a clip of my InputManager... maybe I am using it wrong?
    upload_2018-12-16_23-4-55.png
     
  4. BellokTV

    BellokTV

    Joined:
    Dec 14, 2018
    Posts:
    17
    Ok... I may have found the answer elsewhere before this post even finished going public =)

    The answer may be that I am using GetKey instead of GetButton...
     
    AndreiKope likes this.
  5. firestorm185

    firestorm185

    Joined:
    Sep 23, 2014
    Posts:
    23
    So I found out it was because in my case I was calling the string "Space" and not "Jump"
     
  6. AndreiKope

    AndreiKope

    Joined:
    Aug 8, 2020
    Posts:
    1

    same issue GetButton solved it,
     
    BellokTV likes this.
  7. TimStrijbos

    TimStrijbos

    Joined:
    Nov 7, 2021
    Posts:
    1
    yup, GetButton did the trick.
     
    BellokTV likes this.
  8. BellokTV

    BellokTV

    Joined:
    Dec 14, 2018
    Posts:
    17
    I am glad that my headache helped. lol! ;)
     
  9. UnnaturalShadows

    UnnaturalShadows

    Joined:
    Aug 30, 2023
    Posts:
    1
    youre so cool thank you for sharing this