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 GarberGames · Mar 13, 2017 at 06:56 AM · scripting problemcollisioncharacterjump

Why can't my character jump

if anyone can tell me why my code won't work, plz do... im following a tutorial by CasanisPlays on episode 7 and my code is the exact same as his yet my character will not jump. here is the code

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class playercontroller : MonoBehaviour {

 //movement variables
 public float maxSpeed;

 //jumping variables
 bool grounded = false;
 float groundCheckRadius = 0.5f;
 public LayerMask groundLayer;
 public Transform groundCheck;
 public float jumpHeight;

 Rigidbody2D myRB;
 Animator myAnim;
 bool facingRight;

 // Use this for initialization
 void Start () {
     myRB = GetComponent<Rigidbody2D>();
     myAnim = GetComponent<Animator>();

     facingRight = true;
     
 }
 
 // Update is called once per frame
 void update()
 {
     if (grounded && Input.GetAxis("Jump") > 0)
     {
         grounded = false;
         myAnim.SetBool("isGrounded",grounded);
         myRB.AddForce(new Vector2(0,jumpHeight));
     }
 }


 void FixedUpdate () {

     //check if we are grounded - if no, then we are falling
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
     myAnim.SetBool("isGrounded", grounded);

     myAnim.SetFloat("verticalSpeed", myRB.velocity.y);


     float move = Input.GetAxis("Horizontal");
     myAnim.SetFloat("speed", Mathf.Abs(move));

     myRB.velocity = new Vector2(move * maxSpeed, myRB.velocity.y);

     if (move > 0 && !facingRight)
     {
         flip();
     }  else if (move < 0 && facingRight)
     {
         flip();
     }
 }

 void flip()
 {
     facingRight = !facingRight;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }

}

I have tried everything. my character has a jump force and the animations all work but nothing happens when i press W or Up

Comment
Add comment · Show 3
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
avatar image SnStarr · Mar 13, 2017 at 07:24 AM 0
Share

Only thing I see that might make a difference or might not....is

 Input.GetAxis("Jump") 

not sure if Jump has an axis...I think you want

 Input.GetAxis("Vertical")
avatar image logicandchaos · Feb 09, 2021 at 02:57 PM 0
Share

Are you sure this is getting the right result? Try debugging the value.

grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);

maybe groundCheckRadius is too small?

avatar image Jesse2431 · Feb 09, 2021 at 04:04 PM 0
Share

I would try setting the jumpheight higer.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by AbandonedCrypt · Feb 09, 2021 at 01:48 PM

Change your line

 if (grounded && Input.GetAxis("Jump") > 0)

to

 if (grounded && Input.GetKey(KeyCode.Space))

to jump using space bar. Since you did not specify anything about how you want to jump and you say GetAxis("Vertical") did not help you, I just assume u want to use space.

Also check the value of your grounded variable with Debug.Log(), because if that doesn't work properly, your jump can't work anyways.

Also you should control your jump like this: Create a bool (something like doJump). In Update() check for Jump input (like you did) and then set doJump true. In FixedUpdate check if(doJump) and put your jump logic in there, don't forget to set doJump to false. That way you can check for Jump input in Update() but your jump is still executed in FixedUpdate() like it should.

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
avatar image
0

Answer by BartHeq · Mar 14, 2017 at 09:36 AM

@GarberGames , just as @SnStarr said, the problem might be with Jump axis. In Unity editor, go to Edit -> Project Settings -> Input. There you can see your axes and buttons assigned to them. In my case, default button for "Jump" is space. So maybe you are pressing wrong button, because W and Up are buttons for "Vertical" Axis. If you want to jump with W or Up, change your 27th line of code: change "Jump" to "Vertical".

Also, jumping code should be in FixedUpdate(), not Update(), because it uses physics.

Comment
Add comment · Show 1 · 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
avatar image mahnoorcodes · Feb 09, 2021 at 01:28 PM 0
Share

I cant jump still :/

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

9 People are following this question.

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

Related Questions

character jump on collision with a box 0 Answers

2D Platformer with custom physics Object - jumping issue 0 Answers

Character jump on collision 1 Answer

How to check if character reached jump apex / peak 1 Answer

Minor Addition to Jumping using CharaterController 1 Answer


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