V
V
viktorpeskov2019-10-28 19:59:09
2D
viktorpeskov, 2019-10-28 19:59:09

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

1 answer(s)
G
GavriKos, 2019-10-28
@GavriKos

Well, for example, because isGrounded ==false? Such issues are resolved by the debugger.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question