Hey! I’m learning unity but i have a problem. My jump function is not working properly. Not stable at all. I can jump when i play game but then, i cant jump some times. Sorry for my bad English
the codes I use are here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontroller : MonoBehaviour
{
public int speed;
public int jumpSpeed;
Animator animator;
Rigidbody2D rb;
bool canJump = true;
bool faceRight = true;
private void Start()
{
animator = GetComponent();
rb = GetComponent();
}
private void FixedUpdate()
{
float moveInput = Input.GetAxis(“Horizontal”);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(moveInput > 0 || moveInput < 0)
{
animator.SetBool(“isRunning”, true);
}
else
{
animator.SetBool(“isRunning”, false);
}
if (faceRight == true && moveInput < 0)
{
Flip();
}else if(faceRight == false && moveInput > 0)
{
Flip();
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.transform.tag == “Platform”)
{
canJump = true;
}
}
private void Jump()
{
if (canJump)
{
rb.AddForce(Vector2.up * jumpSpeed);
canJump = false;
}
}
private void Flip()
{
faceRight = !faceRight;
Vector3 scaler = transform.localScale;
scaler.x *= -1;
transform.localScale = scaler;
}
}