Answer the question
In order to leave comments, you need to log in
How to implement a jump in a 2D game?
I tried to implement a jump in a 2D game and at the start of the project the character flew into the sky without pressing the spacebar
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehavior
{
public Rigidbody2D physic;
private bool isGrounded;
private float groundRadius = 0.7f;
public Transform GroundCheck;
public LayerMask groundMask;
void Start()
{
}
void Update()
{
transform.Translate(Vector3.right * 0.1f);
isGrounded = Physics2D.OverlapCircle(GroundCheck.position, groundRadius, groundMask);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) ;
{
physic.AddForce(new Vector2(0, 10));
}
} }
Answer the question
In order to leave comments, you need to log in
Firstly, why is there a ";" after the condition for the click test? Second, add to addforce the second parameter forcemode.impulse
void Update()
{
transform.Translate(Vector3.right * 0.1f);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) ;
{
physic.AddForce(new Vector2(0, 10));
isSpace=true;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question