Hello, I tried to jump when I pressed the space bar, but it doesn’t work. What’s the problem?
help me plz…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float jumpForce = 10f;
public float jumpForce2 = 12f;
private int jumpCount = 0;
private bool isGrounded = false;
Rigidbody2D Rigidbody;
Animator animator;
private void Start() {
Rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
private void Update() {
PlayerAni_Jump();
}
private void PlayerAni_Jump() {
if (Input.GetKeyDown(KeyCode.Space)&&jumpCount < 3) {
jumpCount++;
Rigidbody.velocity = Vector2.zero;
Rigidbody.AddForce(Vector2.up * jumpForce);
}
else if(Rigidbody.velocity.y > 0) {
Rigidbody.velocity = Rigidbody.velocity * 0.5f;
animator.SetBool("Grounded", isGrounded);
}
}
void OnCollisionEnter2D(Collision2D collision) {
if(collision.contacts[0].normal.y > 0.7f) {
jumpCount = 0;
}
}