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

Can't jump on tilemap

Discussion in '2D' started by dorek123, Apr 2, 2021.

  1. dorek123

    dorek123

    Joined:
    Mar 27, 2021
    Posts:
    1
    Hi guys,
    I have a problem. My character is stuck on jump animation and can't jump when he is on Tilemap Collider 2D. I changed this to 2 box colliders but it's still not working.

    Here is how it looks:



    Here is code with player input:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_Input : MonoBehaviour
    6. {
    7.     public static bool isJumpPressed = false;
    8.     public static float movementInput;
    9.     public static Vector3 localMousePosition;
    10.     public static Transform playerTransform;
    11.     private Animator playerAnim;
    12.  
    13.     private void Start()
    14.     {
    15.         playerTransform = GetComponent<Transform>();
    16.         playerAnim = GetComponent<Animator>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         movementInput = Input.GetAxis("Horizontal");
    22.  
    23.         if (movementInput != 0f)
    24.         {
    25.             playerAnim.SetFloat("Speed", 0.2f);
    26.         }
    27.         else
    28.         {
    29.             playerAnim.SetFloat("Speed", 0f);
    30.         }
    31.  
    32.  
    33.         localMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    34.  
    35.         if (Input.GetButtonDown("Jump"))
    36.         {
    37.             isJumpPressed = true;
    38.         }
    39.     }
    40. }
    41.  
    And here is code with physics:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using static Player_Input;
    5.  
    6. public class Player_Physics : MonoBehaviour
    7. {
    8.     [SerializeField] private LayerMask platformLayerMask;
    9.     [SerializeField] private Transform groundCheck;
    10.  
    11.     private Rigidbody2D playerBody;
    12.     private Collider2D playerCollider;
    13.     private SpriteRenderer playerSprite;
    14.     private Animator playerAnim;
    15.  
    16.  
    17.  
    18.     [SerializeField] private float playerSpeed = 40f;
    19.     [SerializeField] private float jumpHeight = 2f;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         playerBody = GetComponent<Rigidbody2D>();
    25.         playerCollider = GetComponent<BoxCollider2D>();
    26.         playerSprite = GetComponent<SpriteRenderer>();
    27.         playerAnim = GetComponent<Animator>();
    28.  
    29.     }
    30.  
    31.     private void Update()
    32.     {
    33.         if (isGrounded())
    34.         {
    35.             playerAnim.SetBool("isJumping", false);
    36.         }
    37.         else if(!isGrounded())
    38.         {
    39.             playerAnim.SetBool("isJumping", true);
    40.         }
    41.     }
    42.  
    43.     void FixedUpdate()
    44.     {
    45.         playerBody.velocity = new Vector2(movementInput * Time.deltaTime * playerSpeed, playerBody.velocity.y);
    46.  
    47.         Debug.Log(playerBody.velocity);
    48.  
    49.         if (playerBody.velocity.x < 0)
    50.         {
    51.             playerSprite.flipX = true;
    52.         }
    53.         else if (playerBody.velocity.x > 0)
    54.         {
    55.             playerSprite.flipX = false;
    56.         }
    57.  
    58.         bool onGround = isGrounded();
    59.  
    60.  
    61.         if (isJumpPressed && onGround)
    62.         {
    63.                 playerBody.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);
    64.                 isJumpPressed = false;
    65.         }
    66.     }
    67.  
    68.     private bool isGrounded()
    69.     {
    70.         return Physics2D.OverlapBox(groundCheck.position, new Vector2(playerCollider.bounds.size.x, 0.01f), 0f, platformLayerMask);
    71.     }
    72. }
    73.  
     
  2. JackHolman

    JackHolman

    Joined:
    Jun 28, 2019
    Posts:
    9
    I assume would that your tilemap is on a different layer to the one you are checking to see if the player is grounded.
     
    ApoxFox and Quayza like this.