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

When I press space while jumping, my character flies.

Discussion in '2D' started by Pixelman0195, Feb 8, 2021.

  1. Pixelman0195

    Pixelman0195

    Joined:
    Jan 10, 2021
    Posts:
    6
    I was making a 2D platformer game and when I was testing jumping while my character was in air and I pressed space, it jumped like script detected that character is grounded and let it jump. Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     public float speed;
    9.     public Rigidbody2D rb;
    10.     public SpriteRenderer spriteRenderer;
    11.     public Animator animator;
    12.     private bool canCrouch = false;
    13.     private bool canJump = false;
    14.     public float strikeForce;
    15.     public float jumpForce;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         animator = GetComponent<Animator>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
    27.         {
    28.             animator.SetTrigger("jump");
    29.             rb.AddForce(new Vector2(0, jumpForce * speed));
    30.             canJump = false;
    31.         }
    32.  
    33.         if (Physics2D.Raycast(transform.position, Vector3.down, 0.5f))
    34.         {
    35.             canJump = true;
    36.         }
    37.         else
    38.         {
    39.             canJump = false;
    40.         }
    41. }
    Did you guys had problems like that? How did you solve this? Please, help me.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    By fly do you mean the object is just jumping very high? If so, just increase the mass of the rigidbody2d on the object or decrease the jumpForce. I noticed you are multiplying jumpForce by Speed, which is probably another reason it is flying so high.

    Also, i suggest using an Impulse force, like this: rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
     
    MelvMay likes this.
  3. Pixelman0195

    Pixelman0195

    Joined:
    Jan 10, 2021
    Posts:
    6
    Thanks for advice! I used impulse force, but still I can fly with my character. You can see what I mean by "fly" on video.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Ahhh you mean you can continuously keep pressing jump. You need a bool to check if the player is grounded or not. Check out this great video to get a nice 2D player movement set up:



    It is a great foundation you can build upon and will save you frustration as you try to add dashes, double jump, attacks, etc. to your script.
     
  5. Pixelman0195

    Pixelman0195

    Joined:
    Jan 10, 2021
    Posts:
    6
    Thanks. I edited my character's movement, but it still can "fly", but I found out that there's something wrong with my background. When I moved my character on a normal floor it couldn't "fly", but when there's a background - it can. For Background I used tilemap.

    Edit: I fixed the problem. My background had a tilemap collider, I guess it was added by an accident. Thank you for helping me.
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    It probably means your tiles that form your background are set to the same layer as the ground would be. Basically, the ground or floor you walk and jump on should have the layer or tag that determines if the player is grounded. The background tiles should NOT have that same layer because if they do, then they are probably enabling your player to "be grounded". I hope that makes sense.
     
  7. Pixelman0195

    Pixelman0195

    Joined:
    Jan 10, 2021
    Posts:
    6
    I fixed the problem. My background had a tilemap collider, I guess it was added by an accident. Thank you for helping me.
     
    Cornysam likes this.