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

Resolved Space not working?

Discussion in 'Scripting' started by Guack_, Aug 6, 2020.

  1. Guack_

    Guack_

    Joined:
    Aug 3, 2020
    Posts:
    7
    Hi im having trouble with jumping nothing works for me
    i tried:
    Code (CSharp):
    1.         if(Input.GetKey(KeyCode.Space)){
    2.         rb.AddForce(0,y*Time.deltaTime,0);}
    3.  
    4.         if(Input.GetKey("space")){
    5.          rb.AddForce(0,y*Time.deltaTime,0);}
    6.  
    7.        if(Input.GetKeyDown("space")){
    8.         rb.AddForce(0,y*Time.deltaTime,0);}
    9.  
    nothing seems to work
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You don't need to include
    Time.deltaTime
    with physics operations, since they run at a fixed update interval.

    My guess is the problem isn't with the input not being detected, but the value of
    y * Time.deltaTime
    given to the
    AddForce
    method results in such a tiny number that it is basically 0.
     
    Joe-Censored and KiddUniverse like this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Especially if gravity is on, it will dramatically overpower this tiny amount of force.

    That said we don't have nearly enough of the code to be sure that this is the problem. What function is this line in? Is that function even being run? What is y's value?
     
    Last edited: Mar 1, 2021
  4. Guack_

    Guack_

    Joined:
    Aug 3, 2020
    Posts:
    7
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {  
    7.     public int x,y,z;
    8.     public Rigidbody rb;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {  
    16.         if(Input.GetKey("w")){
    17.         rb.AddForce(0,0,z*Time.deltaTime);}
    18.         if(Input.GetKey("a")){
    19.         rb.AddForce(-x*Time.deltaTime,0,0);}
    20.         if(Input.GetKey("s")){
    21.         rb.AddForce(0,0,-z*Time.deltaTime);}
    22.         if(Input.GetKey("d")){
    23.         rb.AddForce(x*Time.deltaTime,0,0);}
    24.         if(Input.GetKeyDown("space")){
    25.         rb.AddForce(0,y*Time.deltaTime,0);}
    26.     }
    27. }
    28.  
    the other keys work though, if i change space to g for example, it works
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Do you have gravity? If you have gravity and are sliding along the floor, then your force would have to be large to overcome it and get off the ground.

    As mentioned above, don't use Time.deltaTime in this situation. AddForce takes the time into account already.

    What are the values of x, y, and z? These are int's, which is immediately suspicious to me - an int * float becomes an int, which truncates anything beyond the decimal point, so x*Time.deltaTime would have to be returning 0 unless x > 50ish, for example.

    In any case, definitely always use the KeyCode version of GetKey() - it makes it impossible to mess up stuff like capitalization/spelling in the string version of it.
     
    Bunny83 likes this.
  6. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    On a side note, please look into some general guides about code structure. Your code is quite unreadable to someone on the side and my guess is it will be quite unreadable to you in the future if you decide to come back to it.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You should avoid using GetKey in FixedUpdate. GetKey returns true only on frames the key is held down, but FixedUpdate does not run every frame. So it can and will miss some quick key presses.

    Capture your input in Update, act on it in FixedUpdate.
     
  8. Guack_

    Guack_

    Joined:
    Aug 3, 2020
    Posts:
    7
    i changed them to floats nothing happened
    x = 30
    y = 50
    z = 30


    what i did front,back,left,right then jump with "g" but when i change "g" to space it does nothing
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    OK, that's officially bizarre. The video helps to rule out a lot of possible rookie errors that may cause issues, and leaves us with... uh.... I honestly don't know.

    Is there any chance you have an unusual keyboard setup (where unusual = "not American English" in this context)? I'm grasping at straws a bit.

    Try adding this to your script and seeing what the result is - let's confirm whether you can actually receive spacebar input:
    Code (csharp):
    1.  
    2.         private static readonly KeyCode[] KeyCodes = Enum.GetValues(typeof(KeyCode))
    3.         .Cast<KeyCode>()
    4.         .Where(k => ((int)k < (int)KeyCode.Mouse0))
    5.         .ToArray();
    6.         private void Update()
    7.         {
    8.             if (Input.anyKeyDown)
    9.             {
    10.                 for (int i = 0; i < KeyCodes.Length; i++)
    11.                 {
    12.                     KeyCode kc = KeyCodes[i];
    13.                     if (Input.GetKeyDown(kc))
    14.                     {
    15.                         Debug.Log($"Input detected: {kc.ToString()}");
    16.                     }
    17.                 }
    18.             }
    19.         }
    Press spacebar and see what this outputs. On my computer, I get "Input detected: Space".
     
  10. Guack_

    Guack_

    Joined:
    Aug 3, 2020
    Posts:
    7

    it says: The name 'Enum' does not exist in the current context [Assembly-CSharp]
    i dont know what to do to fix that i just have surface level c# knowledge

    And i dont have a american english keyboard

    I guess it will never be fixed :(
     
    Last edited: Aug 8, 2020
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Add to the top of your script file:
    Code (csharp):
    1. using System;
    2. using System.Linq;
     
  12. Guack_

    Guack_

    Joined:
    Aug 3, 2020
    Posts:
    7
    this is weird the output on the console when i pressed space is:

    Input detected: O
    UnityEngine.Debug:Log(Object)
    PlayerMovement:Update() (at Assets/PlayerMovement.cs:29)

    and the letters are messed up too
    maybe unity is using a different keyboard layout?
     
    Last edited: Aug 10, 2020
    DHotdog85 and PraetorBlue like this.
  13. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Yep, it's a known bug. The bug doesn't happen in built game, only in the editor; just use another key until it's is fixed. :)
     
    DHotdog85 likes this.
  14. Guack_

    Guack_

    Joined:
    Aug 3, 2020
    Posts:
    7
    Sooo i cant do anything about it?
     
    Last edited: Aug 10, 2020
  15. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Work around it until the bug is fixed. As far as bugs go, this one's not too hard to work around, assuming that your spacebar consistently reports as an "O", etc. Something like:
    Code (csharp):
    1. if (Input.GetKeyDown(SpacebarKey() ) {
    2. ....
    3.  
    4.  
    5.  
    6. public static KeyCode SpacebarKey() {
    7. if (Application.isEditor) return KeyCode.O;
    8. else return KeyCode.Space;
    9. }
    Do the same thing for any other key you need to check a KeyCode for. Whenever the bug is fixed, just remove the if/else part and just return KeyCode.Space.
     
    DHotdog85 likes this.
  16. Guack_

    Guack_

    Joined:
    Aug 3, 2020
    Posts:
    7
    well that worked! THANK YOU SO MUCH
     
  17. DHotdog85

    DHotdog85

    Joined:
    Aug 11, 2020
    Posts:
    2
    Hi! Just wanted to hop in and say that I was experiencing this same exact problem. Spacebar reports as "O" in my environment, as well. So, +1 on this workaround being helpful. I was going nuts trying to figure it out. Tried different installs of Unity and Linux, even. *Huge sigh of relief*
    _
    Thank you, so much!
    -Don
     
    Guack_ and StarManta like this.
  18. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    You can also use a version of Unity that doesn't have this problem. As far as I know only the alpha versions from a16 have it.
     
  19. DHotdog85

    DHotdog85

    Joined:
    Aug 11, 2020
    Posts:
    2
    @APSchmidt I've been using 2019.4.7f1 when I encountered the issue. I also tried 2020.1.1f1 and 2018.4.25f1 in my troubleshooting attempts, and had the same issue in those. However, I didn't have a problem when 2019.4 was 2019.4.6-something. I may dig through the version archives, out of curiosity, at some point.
     
  20. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    This one is not affected as far as I'm concerned. :)
     
    DHotdog85 likes this.
  21. unity_xW6sO8ps9h_vPA

    unity_xW6sO8ps9h_vPA

    Joined:
    Aug 11, 2020
    Posts:
    3
    is this was already fix?
     
  22. dvoidis

    dvoidis

    Joined:
    Apr 11, 2016
    Posts:
    4
    So i'm still experiencing this issue in 2019.4.9f1 , I find it rather strange that a bug like this is not fixed immediately. It annoys me that so much time is spend on new features, but regression bugs like this takes ages to be fixed.
     
  23. garrett456

    garrett456

    Joined:
    Mar 13, 2013
    Posts:
    6
    Same in 2019.4.7f1. Had to code in a check to see if its was Windows or Linux. At least this thread pointed out what Unity was doing. Thanks
     
  24. TheShinigami6

    TheShinigami6

    Joined:
    Jan 19, 2021
    Posts:
    4
    Hey So for the time being can I just switch the spacebar with another key, and before exporting change it back to Spacebar,
    and if this but is present, will it affect the space input of the exported game?
    Im about to pull my hair off.
     
  25. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The best way is to use platform dependent compilation to check if you're in the editor or not. If you're in the editor you would then check against a different key.

    Code (csharp):
    1. #if UNITY_EDITOR
    2.     //Check against some other key only in the editor
    3.  
    4. #else
    5.     //Check for space key, or whatever key you want in the build
    6.  
    7. #endif
    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
     
  26. TheShinigami6

    TheShinigami6

    Joined:
    Jan 19, 2021
    Posts:
    4