I’m making a test game (this is my first experience with unity) and I am attempting to make my character jump. I am getting this code from a tutorial video by the way, and I can move from right to left just fine, but my player will not jump, please help.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public float speed;
public float jumpForce;
private float moveInput;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(!facingRight && moveInput > 0)
{
Flip();
}
else if(facingRight && moveInput < 0)
{
Flip();
}
}
void update()
{
if(isGrounded)
{
extraJumps = extraJumpsValue;
}
if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
images: