Ok so im very new to coding, and i’ve been trying to make a movement script for my 2D player, everything is going good except for the jumping part of the script , so in the jumping part when i Jump the movement along the x Axis stops moving, so whilst im running and Jump he just stops and jumps no movement on the X axis whilst in air
This is my Script
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
public int Speed;
private Animator PlayerAnimation;
public bool facingRight = false;
private float MoveX;
public bool sprintToggle = false;
public float JumpForce;
public bool IsGrounded;
void Start()
{
facingRight = false;
sprintToggle = false;
PlayerAnimation = GetComponent<Animator>();
Speed = 13;
JumpForce = 1250f;
}
private void OnCollisionEnter2D(Collision2D collision) //DETECTS IF PLAYER IS ON GROUND
{
if(collision.gameObject.tag == "Ground")
{
IsGrounded = true;
}
}
private void OnCollisionExit2D(Collision2D collision) // DETECTS IF PLAYER IS OFF OF GROUND
{
if(collision.gameObject.tag == "Ground")
{
IsGrounded = false;
}
}
void Update()
{
PlayerMove();
Moving();
SprintToggle();
Jump();
}
public void PlayerMove() //MOVES HORIZONTALLY AND FLIPS HORIZONTALLY
{
if (IsGrounded == true)
{
MoveX = Input.GetAxis("Horizontal");
{
var x = MoveX * Time.deltaTime * Speed;
{
transform.Translate(x, 0, 0);
}
}
if (MoveX < 0.0f && facingRight == true)
{
FlipPlayer();
}
if (MoveX > 0.0f && facingRight == false)
{
FlipPlayer();
}
}
}
public void Moving() //CHECKS IF PLAYER IS MOVING & Walking & Running Animation
{
if (Input.GetKey("left")&&IsGrounded == true || Input.GetKey("right") && IsGrounded == true || Input.GetKey("left") && Input.GetKey("right") && IsGrounded == true || Input.GetKey("a") && IsGrounded == true || Input.GetKey("d") && IsGrounded == true || Input.GetKey("a")&& Input.GetKey("d") && IsGrounded == true)
{
if (Speed <= 13)
{
PlayerAnimation.SetBool("Moving", true);
PlayerAnimation.SetBool("Running", false);
}
else if (Speed > 13)
{
PlayerAnimation.SetBool("Running", true);
PlayerAnimation.SetBool("Moving", false);
}
}
else if (Input.GetKeyUp("left") || Input.GetKeyUp("right")|| Input.GetKeyUp("a") || Input.GetKeyUp("d"))
{
PlayerAnimation.SetBool("Moving", false);
PlayerAnimation.SetBool("Running", false);
}
if (IsGrounded == false)
{
PlayerAnimation.SetBool("Moving", false);
PlayerAnimation.SetBool("Running", false);
}
}
public void FlipPlayer() //FLIPS PLAYER HORIZONTALLY
{
facingRight = !facingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
}
public void SprintToggle() //Toggles Sprint [r]
{
if (Input.GetKeyDown("r"))
{
sprintToggle = !sprintToggle;
if (sprintToggle == false)
{
Speed = 13;
}
else if (sprintToggle == true)
{
Speed = 30;
}
}
}
public void Jump() // JUMPS
{
if (Input.GetKeyDown("space")&&IsGrounded == true)
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up * JumpForce);
}
}
}