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 /
  • Help Room /
avatar image
0
Question by carringtonz · Dec 04, 2016 at 11:37 PM · jumpstuckinconsistent

My jumping in my game is inconsistent

My characters jumps are inconsistent, sometimes I have to press spacebar multiple times for it to jump while other times it only takes one try. I assumed it was because I was moving so I added more functionality into the if statement in the handleInput function to try and cope with it, but this does not fix it. Another problem I am having is after I jump sometimes after I land I get stuck in place, but if I jump again and land it unsticks itself, very strange :( . Please help. Here is my code:

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour { private Rigidbody2D RigidBody; //A private varible for RigidBody2D to be stored in

 private Animator Animation;
 //Varible to store the animator from unity

 [SerializeField]
 private float movementSpeed;
 //Varible to store speed of the player

 private bool FacingRight;

 [SerializeField]
 private Transform[] GroundPoints;

 [SerializeField]
 private float GroundRadius;

 [SerializeField]
 private LayerMask WhatIsGround;

 private bool isGrounded;

 private bool jump;

 [SerializeField]
 private bool AirControl;

 [SerializeField]
 private float jumpForce;


 // Use this for initialization
 void Start() {

     FacingRight = true;

     RigidBody = GetComponent<Rigidbody2D>();
     //Getting RigidBody2D and storing it in the RigidBody varible

     Animation = GetComponent<Animator>();
     //Getting animator from unity and storing it in the animation varible

 }

 // Update is called once per frame
 void FixedUpdate()
 {
     isGrounded = IsGrounded();

     float Horizontal = Input.GetAxis("Horizontal");
     //Creates a reference for Unity called Horizontal

     Movement(Horizontal);
     //Calls the movement function

     Flip(Horizontal);

     HandleInput();

 }
 private void Movement(float Horizontal)
 {
     if (isGrounded || AirControl)
     {
         RigidBody.velocity = new Vector2(Horizontal * movementSpeed, RigidBody.velocity.y);
     }
     //Makes the player go either left or right depending what key is pressed leaving the y unchanged
     Animation.SetFloat("Speed", Mathf.Abs(Horizontal));
     //Sets the Speed parameter in unity to the horizontal, however the math function makes sure it always
     //positive so it doesn't return a negative speed
     if (isGrounded && jump)
     {
         isGrounded = false;
         RigidBody.AddForce(new Vector2(0, jumpForce));
         jump = true;
         jump = false;
     }
 }

 private void HandleInput()
 {
     if ((Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.LeftArrow)) || (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.RightArrow)) || ((Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.D)) || (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.A)) || (Input.GetKeyDown(KeyCode.Space))))
         {
             jump = true;
             Debug.Log("Space key was pressed.");
         }
 }


 private void Flip(float Horizontal) {
     if (Horizontal > 0 && !FacingRight || Horizontal < 0 && FacingRight) {
         FacingRight = !FacingRight;
         // If the Sprite is facing right and the FacingRight varible is not true or
         //it is facing left and the FacingRight varible is set to true it will make the
         //FacingRight varible the opposite of what it was before ie. False to true or true to false

         Vector3 TheScale = transform.localScale;
         //Gets the scale from unity and stores it in a varible so it can refrenced

         TheScale.x *= -1;
         //The scale is made the inverse of what it was before (1 to -1 or -1 to 1) flipping the Sprite

         transform.localScale = TheScale;
     }
 }

 private bool IsGrounded()
 {
     if (RigidBody.velocity.y <= 0)
     //if the player is falling or still
     {
         foreach (Transform point in GroundPoints)
         {
             Collider2D[] Colliders = Physics2D.OverlapCircleAll(point.position, GroundRadius, WhatIsGround);

             for (int i = 0; i < Colliders.Length; i++)
             {
                 if (Colliders[i].gameObject != gameObject)
                 {
                     return true;
                 }
             }

         }
     }
     return false;
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by carringtonz · Dec 14, 2016 at 01:02 PM

I fixed it, I didn't call handleinput in update, I called it in fixedupdate. You have to make a separate function called update, because fixedupdate doesn't run every frame, hence the reason why my jumps were so inconsistent.

Comment
Add comment · Share
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

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

78 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make 3D character controller not stuck on walls 0 Answers

Animation stuck in jump whenever I play. 0 Answers

Character Controller Inconsistent jump 1 Answer

Unity2D Player Jump Heights Inconsistent,Unity2D Inconsistent Player Jump Heights 0 Answers

Make a rigidbody Jump (global up) 3 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