Jump and movement doesn't works simultaneously
Hallo
I try to make a game. In this game my background should move automatically forward. I did with vilocity. This also work. Then I tried to program the jump. I also programed it with vilocity. The jump also works if the part which should move my caracter forward doesn't run.
What do I have to change that my character moves forward and he also can jump?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerJump : MonoBehaviour
{
public float jumpForce = 10f;
private Rigidbody2D myRB;
public float speed = 2;
void Start()
{
myRB = transform.GetComponent<Rigidbody2D>();
}
public void FixedUpdate()
{
myRB.velocity = Vector2.right * speed;
}
public void Jump()
{
myRB.velocity = new Vector2(myRB.velocity.x, jumpForce);
}
}
Answer by Pyroshiv · Aug 24, 2020 at 09:51 AM
The pb is probably that in fixed update you say "run" but when you are jumping,you replace the vector "run" by "jump",but you want to say"run AND jump". I think that you should merge the two vectors when you are jumping.like you make a bool variable called jump and set it to true when you are jumping:
" if (jump)
{
myRB.velocity = new Vector2(right * speed,jumpForce);//right*speed->"run",jumpForce->"jump"
}
else { myRB.velocity = Vector2.right * speed;
} "
that should works,making the character jumping and running at the same time(That can make a weird thing but you will be able to modify the variable to make it better)
Thx for your help. I think that was one of the ploblems.
The otherone was that I wrote Vector2.right ==> Vector2(1, 0). I had to write my own Vector2 because of the 0 in the Y Axis.
Your answer
Follow this Question
Related Questions
Fix for inconsistent jump height? 1 Answer
Issue using WASD controls in FPS microgame template 0 Answers
Player stuck when jumping into a wall 0 Answers
Character sometimes moves faster when I jump and start moving at the same time 1 Answer
How do I assign a velocity to an object on only 1 axis? 1 Answer