M
M
Mimocodil2021-04-12 15:18:26
Unity
Mimocodil, 2021-04-12 15:18:26

How to make jump and fall speed the same?

I'm making a 2d platformer. Rigidbody2d hangs on the character. You need to make a move.
The jump is implemented with rb.AddForce(Vector2.up * jump_power); where rb is a rigidbody and jump_power is a public float.
The character jumps, but jumps low. By setting the jump_power value, I was able to ensure that the character jumps to the desired height, however, he does it abruptly, and falls very slowly. After playing around with the gravity and mass values, I achieved a normal fall speed, but this does not affect the jump speed. Can you tell me how can I change the code to solve this problem?

Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {

  private Rigidbody2D rb;

  public float move_speed = 10.0f;
  public float jump_power = 4000f;

    void Start() {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update() {
        float moveX = Input.GetAxis("Horizontal");

    rb.MovePosition(rb.position + Vector2.right * moveX * move_speed * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.Space))
      rb.AddForce(Vector2.up * jump_power);
    }
}


UPD: Solution from another forum that helped me: use velocity instead of MovePosition:
rb.velocity = new Vector2(moveX * move_speed, rb.velocity.y);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kokapuk, 2021-04-12
@kokapuk

You can change the force of gravity Edit -> Project Settings -> Physics 2D and there the first item, gravity, rotate the y value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question