Search Unity

Problems with Input and jumping

Discussion in 'Editor & General Support' started by CrystalMoose, Mar 11, 2019.

  1. CrystalMoose

    CrystalMoose

    Joined:
    Dec 7, 2018
    Posts:
    4
    [UNSOLVED]

    Hello everyone! i have an issue that's been bothering me a lot. So, until recently, i had a jumping function in FixedUpdate that both detected the key and applied physics to the object in question. However, i decided it was more optimal to move the key-detecting over to Update because of various times where i tried to jump and FixedUpdate just didn't detect it in time. Horrible decision. Immediately upon doing so, i noticed pressing W does absolutely nothing when the player reaches the floor, furthermore, i can also jump with Spacebar? even though that was a thing i removed long ago. Please help.

    Code (CSharp):
    1.  
    2.  
    3. void Update()
    4.     {
    5.         if (direction && speed >= 0) {
    6.         } else {
    7.            if (speed < 0 && !direction) {return;}
    8.  
    9.            speed = -speed;
    10.         }
    11.  
    12.         if (Input.GetButtonDown("W")) {
    13.             if (!jump) {
    14.                 jump = true;
    15.             }
    16.             if (touchingWall) {
    17.                 jump = true;
    18.                 if (direction) {
    19.                     direction = false;
    20.                 } else {
    21.                     direction = true;
    22.                 }
    23.             }
    24.         }
    25.     }
    26.  
    27.     void FixedUpdate()
    28.     {
    29.         Vector3 targetVelocity = new Vector2(speed * Time.fixedDeltaTime * 10, m_Rigidbody2D.velocity.y);
    30.         m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
    31.  
    32. //Code for restarting with R, not important to the issue
    33.  
    34.         if (jump) {
    35.             jump = false;
    36.             m_Rigidbody2D.AddForce(transform.up * thrust);
    37.         }
    38.     }
    39.  
    40.     void OnCollisionEnter2D(Collision2D col)
    41.     {
    42.         m_Rigidbody2D.gravityScale = 1;
    43.         if (jump) {
    44.             jump = false;
    45.         }
    46.         if (col.gameObject.name == "Walls") {
    47.             m_Rigidbody2D.gravityScale = 0.2f;
    48.             touchingWall = true;
    49.         }
    50.     }
    NOTE: Variables are already defined, you might also find some unrelated code, please don't mind it.
    NOTE 2: Although unrelated to this issue[?], my game ending mechanic has also been working weird since i made this change
    NOTE 3: After further researching, i have found only walljumps vanish my ability to jump
     
    Last edited: Mar 13, 2019
  2. CrystalMoose

    CrystalMoose

    Joined:
    Dec 7, 2018
    Posts:
    4