- Home /
Im trying to make my character jump while me also having the option to double jump, but im getting an error. can someone review the code please?
So im trying to get my character to jump while me having the option for him to double jump if i change my mind later but when writing the code it says i have an error, doesnt specify the error it just says i have an error in my playercontroller script so can someone help debug this code because i cant.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public float speed; public float jumpForce; private float moveInput;
private Rigidbody2D rb;
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.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(facingRight == false && moveInput > 0)
{
Flip();
} else if(facingRight == true && moveInput < 0)
{
Flip();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
} else(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps++;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
so in the if statement on the input key for space i think thats where im having the error because before i was able to jump but when i landed on the ground i couldn't jump again and all the movement script is good, so please help i would appreciate it a lot. or maybe just recommend a tutorial for 2d platform jumping, anything helps. -Thanks
Your answer
Follow this Question
Related Questions
Errors CS1513 and CS1022 - Need help. 1 Answer
Why am I seeing: error CS0117: `Debug' does not contain a definition for `LogWarning' 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity logs an error whenever I try to deallocate a nativearray, is there a proper way to do it? 1 Answer