L
L
likhan2020-04-19 21:04:04
C++ / C#
likhan, 2020-04-19 21:04:04

How to do a combination of movement and jump?

In general, the simultaneous movement and jump works, but after I still jump like this, the jump simply does not work for me (I pressed the space bar 100 times). Help me please.

void Start () {
      rb = GetComponent<Rigidbody2D>();
  }
  void Update()
  {
    if (Input.GetKeyDown(KeyCode.Space))
    {
      jump();
    }
  }
  void FixedUpdate () {
    float speed = 4f;
    if (Input.GetKey(KeyCode.D))
    {
      rb.velocity = new Vector2(speed, rb.velocity.y);
    }
    if (Input.GetKey(KeyCode.A))
    {
      rb.velocity = new Vector2(-speed, rb.velocity.y);
    }
  }
  void jump()
  {
    rb.AddForce(transform.up * 4f, ForceMode2D.Impulse);
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SkyCrusher, 2020-04-19
@SkyCrusher

Look at Unity Learn while it's open , they'll chew it all up and put it in your mouth. For example, here ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question