Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by StephanKrosecz · Oct 23, 2016 at 08:23 PM · unity 5unity 2d2d platformer

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;
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Particle System for Death effect 1 Answer

Unable to click/touch event on sprite 1 Answer

Making the player change his movement when it hit an object with collider 0 Answers

Assertion failed on expression: 'goodPathName.find("assets/") != 0' 0 Answers

Unity 2D: Variable Jump Height with a Double Jump 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges