M
M
MistFolk2021-10-26 22:24:46
Unity
MistFolk, 2021-10-26 22:24:46

Randomness of Unity physics?

When I press W the character jumps, but each time to a different height, how to fix it?

void Update()
         {
             Jump();
         }
     
     private void Jump()
         {
             if (Input.GetKeyDown(KeyCode.W))
             {
                 
                 var jumpVel = new Vector2(0, jumpForce * Time.deltaTime);
                 rb.velocity += jumpVel;
                 
             }
         }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
KraGen.Developer, 2021-10-26
@KraGenDeveloper

Basically just do it

void FixedUpdate()
         {
             Jump();
         }
     
     private void Jump()
         {
             if (Input.GetKeyDown(KeyCode.W))
             {
                 
                 var jumpVel = new Vector2(0, jumpForce);
                 rb.velocity += jumpVel;
                 
             }
         }

G
Griboks, 2021-10-27
@Griboks

1) Time.deltaTimehas a different meaning each time.
2)

var jumpVel = new Vector2(0, jumpForce * Time.deltaTime);
- you are trying to add speed instead of strength.
3) rb.velocity += jumpVel;- you add speed, so the sum can be at least zero. I recommend setting the speed hard: rb.velocity = jumpVel;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question