- Home /
Character Jumps When Falling (Unity2D)
Alrighty, I've looked all over the forum for a solution to this problem, but no dice so hopefully someone can help with this.
I have an issue where my player character will fall normally when running off platforms as long as I haven't jumped. However, once I've jumped the character will consistently jump whenever the groundcheck comes in false.
Given that it's safe to assume there's a problem with my jump script that allows this to happen, but my script explicitly states that a button needs to be pressed for that function to work. Nevertheless, whenever I walk off of a platform the character jumps, eliminating the doublejump feature.
I've added the PlayerController code below, things are labelled so it shouldn't be too difficult to read. Big thanks to anyone who can help me out with this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
//Movement Variables
public int jumpHeight;
public int extraJumps;
public Transform groundPoint;
public float radius;
public LayerMask groundMask;
bool isGrounded = false;
bool jumping = false;
bool isFalling;
Rigidbody2D rb2D;
int originalExtraJumps;
//Running with force variables
public float maxSpeed = 10f; //fastest sprite can move
bool facingRight = true;
public float jumpForce = 400f;
Animator anim;
// Use this for initialization
void Start () {
//Custom names
rb2D = GetComponent<Rigidbody2D>();
originalExtraJumps = extraJumps;
anim = GetComponent<Animator> ();
}
//Physics Work
void FixedUpdate () {
//Decides whether or not character is grounded
isGrounded = Physics2D.OverlapCircle (groundPoint.position, radius, groundMask);
//anim.SetBool ("Jump", false);
if (isGrounded) {
anim.SetBool ("grounded", true);
jumping = false;
}
if (!isGrounded)
anim.SetBool ("grounded", false);
//Determine vertical speed
anim.SetFloat ("vSpeed", rb2D.velocity.y);
//Force movement
float move = Input.GetAxis ("Horizontal");
//Sets speed for Animator
anim.SetFloat("Speed", Mathf.Abs(move));
rb2D.velocity = new Vector2 (move * maxSpeed, rb2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
//Falling check
if (rb2D.velocity.y < 0.01) {
isFalling = true;
} else {
isFalling = false;
}
}
// Update is called once per frame
void Update () {
//Jump Script
if (isGrounded && Input.GetButtonDown("Jump")) {
rb2D.AddForce(new Vector2(0, jumpForce));
anim.SetBool ("Jump", true);
extraJumps--;
jumping = true;
}
if (!isGrounded && Input.GetButtonDown("Jump") && extraJumps != 0) {
anim.SetTrigger ("doubleJump");
rb2D.AddForce(new Vector2(0, jumpForce));
extraJumps--;
}
if (isGrounded) {
extraJumps = originalExtraJumps;
}
}
//Sprite flip code
void Flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
//groundPoint visualization
void OnDrawGizmos () {
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (groundPoint.position, radius);
}
void OnCollisionEnter2D(Collision2D other){
if (other.transform.tag == "MovingPlatform") {
transform.parent = other.transform;
}
}
void OnCollisionExit2D(Collision2D other){
if (other.transform.tag == "MovingPlatform") {
transform.parent = null;
}
}
}