- Home /
The question is answered, right answer was accepted
i am trying to make some movement for my character but i am having some issues when it comes to jumping
i am trying to make some movement for my character but i am having some issues when it comes to jumping. I originally had all of my movement and jumping in one file like this:
public Rigidbody2D rb;
public float moveSpeed = 5f;
public float jumpHeight = 30f;
private bool isJumping;
public bool onGround;
private bool movingRight;
private bool movingLeft;
void FixedUpdate()
{
//Activating Movement
if (Input.GetKey(KeyCode.D))
{
movingRight = true;
}
else
{
movingRight = false;
}
if (Input.GetKey(KeyCode.A))
{
movingLeft = true;
}
else
{
movingLeft = false;
}
//Right Movement
if (movingRight == true)
{
rb.velocity = new Vector2(moveSpeed, 0.0f);
}
else if (movingLeft == false)
{
rb.velocity = new Vector2(0.0f, 0.0f);
}
//Left Movement
if (movingLeft == true)
{
rb.velocity = new Vector2(-moveSpeed, 0.0f);
}
else if (movingRight == false)
{
rb.velocity = new Vector2(0.0f, 0.0f);
}
//Stop if Pressing Left and Right At Same Time
if (movingRight == true && movingLeft == true)
{
rb.velocity = new Vector2(0.0f, 0.0f);
}
//Jumping
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Space))
{
isJumping = true;
}
else
{
isJumping = false;
}
if (isJumping == true && onGround == true)
{
rb.velocity = Vector2.up * jumpHeight;
Debug.Log("JUMP!!");
}
}
//Checking if Player is On Ground
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
onGround = true;
}
}
void OnCollisionExit2D(Collision2D collision)
{
onGround = false;
}
}
it works exactly how i want it to except for when i jump it seems like it just abruptly goes to the peak of the jump and then starts going down normal.i kind of fixed this problem by separating the Movement and jumping into 2 different scripts and the jumping works as i want it to, but only when i disable the movement script. Here's my movement script
public Rigidbody2D rb;
public float moveSpeed = 5f;
private bool movingRight;
private bool movingLeft;
void FixedUpdate()
{
//Activating Movement
if (Input.GetKey(KeyCode.D))
{
movingRight = true;
}
else
{
movingRight = false;
}
if (Input.GetKey(KeyCode.A))
{
movingLeft = true;
}
else
{
movingLeft = false;
}
//Right Movement
if (movingRight == true)
{
rb.velocity = new Vector2(moveSpeed, 0.0f);
}
else if (movingLeft == false)
{
rb.velocity = new Vector2(0.0f, 0.0f);
}
//Left Movement
if (movingLeft == true)
{
rb.velocity = new Vector2(-moveSpeed, 0.0f);
}
else if (movingRight == false)
{
rb.velocity = new Vector2(0.0f, 0.0f);
}
//Stop if Pressing Left and Right At Same Time
if (movingRight == true && movingLeft == true)
{
rb.velocity = new Vector2(0.0f, 0.0f);
}
}
and my jumping
public Rigidbody2D rb;
public float jumpHeight = 30f;
private bool isJumping;
public bool onGround;
//Checking if Player is On Ground
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
onGround = true;
}
}
void OnCollisionExit2D(Collision2D collision)
{
onGround = false;
}
void Update()
{
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.Space))
{
isJumping = true;
}
else
{
isJumping = false;
}
//Jump Movement
if (isJumping == true && onGround == true)
{
rb.velocity = Vector2.up * jumpHeight;
Debug.Log("JUMP!!");
}
}
I don't what i'm doing wrong
Answer by Ymrasu · Sep 01, 2020 at 08:58 AM
You are assigning both x and y each time to rb.velocity which cancels out the previously assigned velocity. You'd want to separate the x (left and right) and y (up and down). Assign x and y separately while taking the other into account, like this:
// example movespeed
rb.velocity = new Vector2(movespeed, rb.velocity.y);
// example jumpHeight
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
Thanks that worked. i see what the problem was now and i wont make that mistake again.
Follow this Question
Related Questions
My player is lagging/stuttering when trying to move mid-air(after jumping) 1 Answer
My movement script is kind of broken for jumping (upward force is diminished after first jump) 1 Answer
Making jumping independent of normalized movement? 1 Answer
Character floats upward when looking up and down 0 Answers
isGrounded is always false, even with gravity, how do you fix that? 1 Answer