Answer the question
In order to leave comments, you need to log in
Unity 2D. Why is my character not jumping? Walks along x, but does not jump up. It just ignores the space. Why?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controll : MonoBehaviour
{
public float horizontalSpeed;
float speedX;
public float verticalImpulse;
Rigidbody2D rb;
bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKey(KeyCode.A))
{
speedX = -horizontalSpeed;
}
else if (Input.GetKey(KeyCode.D))
{
speedX = horizontalSpeed;
}
if (Input.GetKeyDown (KeyCode.Space) && isGrounded)
{
rb.AddForce(new Vector2(0, verticalImpulse), ForceMode2D.Impulse);
}
transform.Translate(speedX, 0, 0);
speedX = 0;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = false;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question