Search Unity

cant jump while moving

Discussion in 'Scripting' started by S3Critsss, Jan 14, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    im new to untiy but i always wanted to make games i made a movement script but whenever i try to jump while moving it doesnt let me. here is the script
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.       public float moveSpeed;
    9.       public bool isGrounded = false;
    10.       public bool Ismoving = false;
    11.  
    12.       public void Update()
    13.       {
    14.             Jump();
    15.             Flip();
    16.             Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    17.             transform.position += movement * Time.deltaTime * moveSpeed;
    18.       }
    19.  
    20.       public void Jump()
    21.       {
    22.             if (Input.GetButtonDown("Jump") && isGrounded == true) {
    23.                   gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
    24.             }
    25.       }  
    26.  
    27.       public void Flip()
    28.       {
    29.             if (Input.GetKeyDown(KeyCode.RightArrow)) {
    30.                   transform.eulerAngles = new Vector3(0, 0, 0);
    31.             }else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
    32.                   transform.eulerAngles = new Vector3(0, 180, 0);
    33.             }
    34.       }
    35.  
    36. }
    37.  
    and here is my grounded script
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Grounded : MonoBehaviour
    7. {
    8.     GameObject Player;
    9.  
    10.     void Start()
    11.     {
    12.         Player = gameObject.transform.parent.gameObject;
    13.     }
    14.  
    15.     public void OnCollisionEnter2D(Collision2D collision2D)
    16.     {
    17.         if (collision2D.collider.tag == "Ground") {
    18.             Player.GetComponent<PlayerMovement>().isGrounded = true;
    19.         }
    20.     }
    21.  
    22.     public void OnCollisionExit2D(Collision2D collision2D)
    23.     {
    24.         if (collision2D.collider.tag == "Ground") {
    25.             Player.GetComponent<PlayerMovement>().isGrounded = false;
    26.         }
    27.     }
    28. }
    29.  
    30.  
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You are setting the position of the game object in the update function. What you do in the Jump method is being overridden by that.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'm guessing that, while moving, it's not considering you to be grounded. Maybe your object is just sort of skipping over the ground, bouncing imperceptibly, but only actually grounded for a small percentage of the time?

    First thing I would do is, don't mix your transform.position movement with physics movement. Combining the two systems is begging for weird interactions (and this bug might be one such interaction). Alter your rigidbody's velocity for your left-right movement instead. You'll want to preserve the Y component (so that the jumping inertia isn't immediately lost), which you can do like this:
    Code (csharp):
    1. Rigidbody2D rb = GetComponent<Rigidbody2D>();
    2. rb.velocity = new Vector2(movement.x * moveSpeed, rb.velocity.y);
    So, do that first. If that doesn't fix it, there are a couple of other things you can try. Probably the easiest would be a time-based solution: rather than "Am I grounded right now?", ask "Have I been grounded within the last 0.05 seconds?" (If you pay close attention a LOT of platform games have this sort of thing - you get a small number of frames after actually walking off the edge of a platform before you're not able to jump off of it.) If your object is just skipping along the ground, this would give it some leeway.

    To implement this: In addition to setting a isGrounded bool, your script would set a wasGroundedAt float to Time.time every frame the object is grounded. Then, you'll be able to check "if (Time.time < wasGroundedAt + 0.05f)", and that would be your new check for being grounded.

    It's not being overridden, it's being added to. In transform it's using +=, so any changes to the position made by the rigidbody will still be there after that line.
     
  4. yeevon234

    yeevon234

    Joined:
    Apr 6, 2020
    Posts:
    1
    problem solved?
     
  5. unity_FE2ek-I0sV9w0w

    unity_FE2ek-I0sV9w0w

    Joined:
    Sep 26, 2020
    Posts:
    1
    I solve the problem in update
    u must make jump code the last code in update
     
  6. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Maybe set the isGrounded boolean to false after you jumped rather than checking it via aonCollisionExit2D