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

keycode not working

Discussion in 'Getting Started' started by Large420, May 8, 2020.

  1. Large420

    Large420

    Joined:
    May 8, 2020
    Posts:
    3
    My speed is not changing at all but when i put print("test") in the if statement it will print test before i had: Input.getbutton(fire3) since it was set as the left shift after that i changed to this code: upload_2020-5-8_21-55-24.png
    This is the full code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharMove : MonoBehaviour
    6. {
    7.     //link the character controller
    8.     public CharacterController controller;
    9.     //set movment speed
    10.     public float speed = 10f;
    11.  
    12.     Vector3 velocity;
    13.     //set gravity for the player
    14.     public float gravity = -10f;
    15.  
    16.     //link groundcheck
    17.     public Transform Groundcheck;
    18.     public float GroundDistance = 0.4f;
    19.     public LayerMask Groundmask;
    20.     public float jump = 3f;
    21.  
    22.     bool isGrounded;
    23.  
    24.     void Update()
    25.     {
    26.  
    27.         isGrounded = Physics.CheckSphere(Groundcheck.position, GroundDistance, Groundmask);
    28.  
    29.         if (isGrounded && velocity.y < 0)
    30.         {
    31.             velocity.y = -2;
    32.         }
    33.         //W +1y S-1y    A-1x D+1x!!!caps H and V!!!
    34.         float x = Input.GetAxis("Horizontal");
    35.         float z = Input.GetAxis("Vertical");
    36.  
    37.         Vector3 move = transform.right * x + transform.forward * z;
    38.  
    39.         controller.Move(move * speed * Time.deltaTime);
    40.  
    41.         velocity.y += gravity * Time.deltaTime;
    42.  
    43.         controller.Move(velocity * Time.deltaTime);
    44.  
    45.         if (Input.GetButton("Jump") && isGrounded)
    46.         {
    47.             velocity.y = Mathf.Sqrt(jump * -2f * gravity);
    48.         }
    49.  
    50.         if (Input.GetKeyDown(KeyCode.LeftShift) && isGrounded){
    51.             speed = 20;
    52.         }
    53.         else
    54.         {
    55.             speed = 10;
    56.         }
    57.     }
    58. }
    59.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    GetKeyDown
    is only true for the first frame that the key was pressed.
    If you want the if-statement to run while the key is being held, use
    GetKey
    instead.
     
    Ryiah and JoeStrout like this.
  3. Large420

    Large420

    Joined:
    May 8, 2020
    Posts:
    3
    Ok il try it out thanks!