• 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 Brandoboy · Aug 23, 2016 at 01:30 AM · c#jumpnot working

Player in Unity refuses to Jump

ok, so I was following a tutorial on how to make a 2d platformer, and after the video that we redid the code for the jump functionality nothing happens when i press spacebar!

code in C#!!

here is the jump behavior code: using UnityEngine; using System.Collections;

 public class JumpBehavior : StateMachineBehaviour {
 
     // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         Player.Instance.Jump = true;
     }
 
     // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
     //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     //
     //}
 
     // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         Player.Instance.Jump = false;
     }
 
     // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
     //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     //
     //}
 
     // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
     //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     //
     //}
 }

and here is the player code:

 using UnityEngine;
 using System.Collections;
 
 public class Player : Character
 {
 
     private static Player instance;
 
     public static Player Instance
     {
         get
         {
             if (instance == null)
             {
                 instance = GameObject.FindObjectOfType<Player>();
             }
             return instance;
         }
 
         set
         {
             instance = value;
         }
     }
 
     [SerializeField]
     private Transform[] groundPoints;
 
     [SerializeField]
     private float groundRadius;
 
     [SerializeField]
     private LayerMask whatIsGround;
 
     [SerializeField]
     private bool airControl;
 
     [SerializeField]
     private float jumpForce;
 
     public Rigidbody2D MyRigidbody { get; set; }
 
     public bool Slide { get; set; }
     public bool Jump { get; set; }
     public bool OnGround { get; set; }
 
     // Use this for initialization
 
     public override void Start()
     {
 
         base.Start();
 
         MyRigidbody = GetComponent<Rigidbody2D>();
 
     }
 
     void Update()
     {
         handleInput();
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         float horizontal = Input.GetAxis("Horizontal");
 
         OnGround = IsGrounded();
 
         HandleMovement(horizontal);
 
         Flip(horizontal);
 
         HandleLayers();
 
     }
 
     private void HandleMovement(float horizontal)
     {
         if (MyRigidbody.velocity.y < 0)
         {
             myAnimator.SetBool("land", true);
         }
         if (!Attack && !Slide && (OnGround || airControl))
         {
             MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
         }
         if (Jump && MyRigidbody.velocity.y == 0)
         {
             MyRigidbody.AddForce(new Vector2(0, jumpForce));
         }
 
         myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
 
     }
 
     private void handleInput()
     {
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             myAnimator.SetTrigger("jump");
         }
 
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             myAnimator.SetTrigger("attack");
         }
 
         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
             myAnimator.SetTrigger("slide");
         }
 
         if (Input.GetKeyDown(KeyCode.Mouse1))
         {
             myAnimator.SetTrigger("throw");
         }
 
     }
 
     private void Flip(float horizontal)
     {
         if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
         {
             ChangeDirection();
         }
     }
 
     private bool IsGrounded()
     {
         if (MyRigidbody.velocity.y <= 0)
         {
             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;
 
     }
 
     private void HandleLayers()
     {
         if (!OnGround)
         {
             myAnimator.SetLayerWeight(1, 1);
         }
         else
         {
             myAnimator.SetLayerWeight(1, 0);
         }
     }
 
     public override void ThrowKnife(int value)
     {
 
         if (!OnGround && value == 1 || OnGround && value == 0)
         {
             base.ThrowKnife(value);
         }
     }
 
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

222 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 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 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

how do I make my character jump while running without losing all your speed 1 Answer

jump code character can jump twice please any one fix this 0 Answers

NullReferenceException: Object reference not set to an instance of an object OptionsController.Update () (at Assets/Scripts/OptionsController.cs:26) 1 Answer

Buttons is not working correct on android 0 Answers

What am i doing wrong with my jump attack ? 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