Home > Mobile >  Input.GetKeyDown is won't work properly Unity 2D
Input.GetKeyDown is won't work properly Unity 2D

Time:11-02

I'm trying to make my square jump in Unity 2D when I press Space button. I have the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public float moveSpeed;
    public float jumpSpeed;
    float moveX;
    Vector2 pos;

    // Update is called once per frame
    void Update()
    {
        MoveHorizontally();
        Jump();
    }
    void MoveHorizontally(){
        moveX = Input.GetAxis("Horizontal") * Time.deltaTime;
        pos.x = moveX * moveSpeed;

        transform.position = new Vector2(transform.position.x   pos.x,transform.position.y   pos.y);
    }
    void Jump(){
        if (Input.GetKeyDown("space")){
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0,jumpSpeed * Time.deltaTime), ForceMode2D.Impulse);
            Debug.Log("Jumped!");
        }
    }
}

When I press Space button, "Jumped!" message shows up, but my square not jumping. Any idea?

Thanks a lot.

I tried using Input.GetKey function. It works, but that function keeps making my square go upwards if i keep holding Space button.

CodePudding user response:

U need to assing a value to jumpforce. and of course to the moveforce. if u dont wanna jump repeatly Just name a bool for checking if u are on ground. I use mine as" private bool = isGrounded". and try the code below. It makes ur jump a condition as a function of touching ground.

 private void OnCollisionEnter2D(Collision2D collision)
{

    if (collision.gameObject.CompareTag("Ground")) 
        isGrounded = true;
    

}

and here is my code for jump.

 public void PlayerJump() {

    if (Input.GetButtonDown("Jump") && isGrounded) {
        isGrounded = false;
        myBody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    }

}

whenever u jump it makes your bool variant false and whenever u touch ground it make ur bool true. Make sure "Ground" must be same exactly the tag your ground has.

CodePudding user response:

When you call AddForce() method you don't have to multiply with "Time.deltaTime".

What you can do is the following:

First cached your "Rigidbody2D" component from your Start() method because if you call it every frame it will affect your game performance.

private Rigidbody2D rigidbody2D;

void Start()
{
   rigidbody2D = GetComponent<Rigidbody2D>();
}

Second, instead of Update() method you should call the FixedUpdate() method which is recommended when you deal with Physics

void FixedUpdate()
{
    MoveHorizontally();
    Jump();
}

Third, on your Jump() method as I said on top you should not multiply the jump force with Time.deltaTime, instead do the following:

public float jumpForce = 10f;
public void Jump() 
{
    if (Input.GetKeyDown(KeyCode.Space)) 
    {
        rigidbody2D.AddForce(Vector2.up*jumpForce);
    }
}

You can adjust the jumpForce as you want. Also Vector2.up is a shorthand for writing Vector2(0, 1)

You can read more about Vector2 here Vector2.up Unity's Documentation

  • Related