At the time i can’t hold the spacebar to jump.
Thats the code that i have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
float speed = 2;
public float JumpForce = 4;
private Rigidbody2D _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
var movement = Input.GetAxis("Horizontal");
transform.Translate(Vector2.right * Time.deltaTime * speed);
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y)<0.01f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
}
}