Input.GetButtonDown("Jump") doesn't work
Hello. Space key doesn't work when I run the code. What could be the reason? By the way, the version I'm using is 2021.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float speed = 400f;
private float jumpforce = 500f;
private float OverlapBoxSize = 0.2f;
public Transform FeetPos;
Rigidbody2D Rigidbody;
bool jumpPressed;
void Start()
{
Rigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
Collider2D col = Physics2D.OverlapBox(FeetPos.position, new Vector2(transform.localScale.x, OverlapBoxSize), 0, LayerMask.GetMask("Ground"));
if(Input.GetButtonDown("Jump") && col)
{
jumpPressed = true;
}
}
void FixedUpdate()
{
float HorizontalMovement = Input.GetAxisRaw("Horizontal");
Rigidbody.velocity = new Vector2(HorizontalMovement*speed*Time.deltaTime, Rigidbody.velocity.y);
if(jumpPressed == true)
{
Rigidbody.AddForce(new Vector2(0, jumpforce*Time.deltaTime), ForceMode2D.Impulse);
jumpPressed = false;
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.black;
Gizmos.DrawWireCube(FeetPos.position, new Vector2(transform.localScale.x, OverlapBoxSize));
}
}
Comment
Your answer
Follow this Question
Related Questions
Checking if the player jumps while not grounded 0 Answers
2D Platform irregular jump 1 Answer
How to move a UI Image in the z axis? 2D 0 Answers
Need help with Character Jump! 0 Answers