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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Input : MonoBehaviour
{
public static bool isJumpPressed = false;
public static float movementInput;
public static Vector3 localMousePosition;
public static Transform playerTransform;
private Animator playerAnim;
private void Start()
{
playerTransform = GetComponent<Transform>();
playerAnim = GetComponent<Animator>();
}
void Update()
{
movementInput = Input.GetAxis("Horizontal");
if (movementInput != 0f)
{
playerAnim.SetFloat("Speed", 0.2f);
}
else
{
playerAnim.SetFloat("Speed", 0f);
}
localMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetButtonDown("Jump"))
{
isJumpPressed = true;
}
}
}
And here is code with physics:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Player_Input;
public class Player_Physics : MonoBehaviour
{
[SerializeField] private LayerMask platformLayerMask;
[SerializeField] private Transform groundCheck;
private Rigidbody2D playerBody;
private Collider2D playerCollider;
private SpriteRenderer playerSprite;
private Animator playerAnim;
[SerializeField] private float playerSpeed = 40f;
[SerializeField] private float jumpHeight = 2f;
// Start is called before the first frame update
void Start()
{
playerBody = GetComponent<Rigidbody2D>();
playerCollider = GetComponent<BoxCollider2D>();
playerSprite = GetComponent<SpriteRenderer>();
playerAnim = GetComponent<Animator>();
}
private void Update()
{
if (isGrounded())
{
playerAnim.SetBool("isJumping", false);
}
else if(!isGrounded())
{
playerAnim.SetBool("isJumping", true);
}
}
void FixedUpdate()
{
playerBody.velocity = new Vector2(movementInput * Time.deltaTime * playerSpeed, playerBody.velocity.y);
Debug.Log(playerBody.velocity);
if (playerBody.velocity.x < 0)
{
playerSprite.flipX = true;
}
else if (playerBody.velocity.x > 0)
{
playerSprite.flipX = false;
}
bool onGround = isGrounded();
if (isJumpPressed && onGround)
{
playerBody.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);
isJumpPressed = false;
}
}
private bool isGrounded()
{
return Physics2D.OverlapBox(groundCheck.position, new Vector2(playerCollider.bounds.size.x, 0.01f), 0f, platformLayerMask);
}
}