Question by
Double_vhe · May 19, 2021 at 03:20 AM ·
movementjump
[SOLVED]My character doesn't move at the beginning of a jump
I've made a game in Unity 2D and I decided to change the way of jumping in my game so the player can jump higher if he hold down jump. But now when I jump my character stop moving on the X absis at the beginning of the jump. I've tried to change again and again my script but I can't find what is causing this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float moveSpeed;
public float jumpTime;
private float moveInput;
private float jumpTimeCounter;
public float jumpForce;
private bool isJumping;
public bool isGrounded;
public bool jump = false;
public Rigidbody2D rb;
private Vector3 velocity = Vector3.zero;
public Animator animator;
public SpriteRenderer spriteRenderer;
public Transform groundCheckLeft;
public Transform groundCheckRight;
public GameObject spawn;
public GameObject player;
void Start()
{
player.transform.position = spawn.transform.position;
}
void FixedUpdate()
{
moveInput = Input.GetAxis("Horizontal");
float horizontalMovement = moveInput * moveSpeed * Time.deltaTime;
MovePlayer(horizontalMovement);
Flip(rb.velocity.x);
float characterVelocity = Mathf.Abs(rb.velocity.x);
animator.SetFloat("Speed", characterVelocity);
}
void Update()
{
isGrounded = Physics2D.OverlapArea(groundCheckLeft.position, groundCheckRight.position);
if (Input.GetButtonDown("Jump") && isGrounded)
{
isGrounded = false;
isJumping = true;
jump = true;
jumpTimeCounter = jumpTime;
}
if (Input.GetButton("Jump") && isJumping)
{
isGrounded = false;
if(jumpTimeCounter > 0)
{
jump = true;
jumpTimeCounter -= Time.deltaTime;
}else{
isJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
void MovePlayer(float _horizontalMovement)
{
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
if (jump == true)
{
rb.velocity = Vector2.up * jumpForce ;
jump = false;
}
}
void Flip(float _velocity)
{
if(_velocity > 0.1f)
{
spriteRenderer.flipX = true;
}else if(_velocity < -0.1f)
{
spriteRenderer.flipX = false;
}
}
}
Comment
Answer by Double_vhe · May 19, 2021 at 05:15 PM
The problem was that I messed up with the "jump" and "isJumping" var
Here is the right code for Update :
void Update()
{
isGrounded = Physics2D.OverlapArea(groundCheckLeft.position, groundCheckRight.position);
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
isGrounded = false;
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetButton("Jump") && isJumping == true)
{
isGrounded = false;
if(jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}else{
isJumping = false;
}
}
if(Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
Your answer
Follow this Question
Related Questions
Basic MOVE and JUMP script (Jump trouble) 2 Answers
About states in coding (Idle, Walk, Jump etc 0 Answers
Grounded and pressing "jump" not working? 1 Answer
Character keeps flying after jumping 0 Answers
This is frustrating 2 Answers