- Home /
I want to autojump while i am holding spacebar?
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);
}
}
}
Comment
Answer by Caeser_21 · Mar 09 at 10:47 AM
You have to use 'GetButton' instead of 'GetButtonDown'... You might also have to make an 'IsGrounded' bool and make it true when the player is touching the ground.
Your answer
Follow this Question
Related Questions
Separating Gameobjects from scripts 3 Answers
Adding new code to built client 0 Answers
ERROR BCE0043 i need help pls 1 Answer
Editor calling a constructor for no apparent reason 1 Answer