- Home /
My code isn't working
I have a game, that you have to dodge blocks, and I am implementing a jump feature.
I have the code
public float upwardForce = 100;
If (Input.GetKey(“up”)) { rb.AddForce(0, 0, upwardForce * Time.DeltaTime); }
It doesn't work, and I can't publish the game until it does. I also want to make it so they will fall back to earth and they can't use it when they are in the air. Thanks. Can someone please help me?
Answer by sean244 · Dec 30, 2018 at 08:52 PM
Try this
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D rb;
private bool jumping;
public float upwardForce = 3f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetButtonDown("Jump"))
jumping = true;
}
private void FixedUpdate()
{
if (jumping)
{
jumping = false;
rb.AddForce(Vector2.up * upwardForce, ForceMode2D.Impulse);
}
}
}
This worked so well! Now is there a way to make it so he can only jump when on the ground and not when in the air.
Do you have a bool that returns if the player is grounded or not?
I am not that good at Unity. So I don't. I'm sorry
Answer by giveson · Dec 30, 2018 at 08:47 PM
At the end of the code where it says if (jumping) it should say if (jumping == true) if not it might be if (jumping = true)
Answer by Collects · Jan 10, 2019 at 12:49 PM
I'm back. I couldn't figure it out, If any has any other answers I would love it!