简体   繁体   中英

Unable to use space bar in character controller with GetKey to make the player jump

I'm new to coding and Unity3d in general. So trying to learn and figure out how things work and what's what I decided to use some code from an online tutorial made by Jason Weimann on his YT channel to get me started with player movement. I downloaded the whole project, opened it in Unity (upgraded the project from Unity 2019.3.4f1 to Unity 2020.1.1f1). Fired up the scene (with few simple environment elements and a player with character controller). Everything works as it supposed to until I press space to make the player jump. So I dived into the code, changed Input.GetKey(KeyCode.Space) to Input.GetKey(KeyCode.E) and it works... I tried with various other keys and all of them work except for the space bar. I also tried Input.GetKeyDown works fine too, but not when the key is specified as Space. Any idea why this might be happening?? Please see the code below, even though I don't think there's anything wrong with it (since any other key works just fine and I wasn't the one to write it LOL)

EDIT Thanks for all the suggestions so far. The author of the code was able to get this code to work with Input.GetKey even though Unity documentation doesn't mention this key code to work with it (only with Input.GetKeyDown and Input.GetKeyUp ). Script works with any other key code. I tried using this code in a totally new project and got the same results as before - can't use space to jump, every other key works. Could it be something in my Unity3D settings?

using UnityEngine;

public class CharacterMove : MonoBehaviour
{
    [SerializeField] private float _moveSpeed = 10f;
    [SerializeField] private float _jumpSpeed = 0.5f;
    [SerializeField] private float _gravity = 2f;
    
    CharacterController _characterController;
    Vector3 _moveDirection;

    void Awake() => _characterController = GetComponent<CharacterController>();
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 inputDirection = new Vector3(horizontal, 0, vertical);
        Vector3 transformDirection = transform.TransformDirection(inputDirection);
        
        Vector3 flatMovement = _moveSpeed * Time.deltaTime * transformDirection;

        _moveDirection = new Vector3(flatMovement.x, _moveDirection.y , flatMovement.z);

        if (PlayerJumped)
            _moveDirection.y = _jumpSpeed;
        else if (_characterController.isGrounded)
            _moveDirection.y = 0f;
        else
            _moveDirection.y -= _gravity * Time.deltaTime;

        _characterController.Move(_moveDirection);
    }
//this is the part that does not work when the KeyCode is set to Space, but works with ANY other key.
    private bool PlayerJumped => _characterController.isGrounded && Input.GetKey(KeyCode.Space); 
}

Typically you want your property getters to be as simple as possible, usually just getting a value that's been cached rather than calculated since calculating the value every time it's asked for is hard on performance.

Try caching the Input.GetKey() command in Update by saving it off in a local variable. Then look at that variable during your FixedUpdate. This has always worked for me and it is considered to be best practice.

As for using Input.GetKey, the Unity documentation tells us that the Space key is detected by Input.GetKeyDown and Input.GetKeyUp, but not Input.GetKey. https://docs.unity3d.com/ScriptReference/Input.GetKey.html

OK. Apparently this is a known bug that doesn't happen in built game, only in the editor. It incorrectly detects inputs. There's no fix AFAIK. Don't know if it's Linux only or if other environments are affected too. Apparently the only way around it is to use a different key or deploy and then test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM