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 Kriffix · Mar 19, 2016 at 10:03 PM · scripting problemscripting beginner2d platformer

Cant implement GetButtonKey correctly

Hello Unity community!

I've been doing graphic design for a while but I've only started coding/scripting for the first time a couple of days ago. I'm watching some tutorials on how to create a 2D sidescroller but have been adding my own twists here and there with bits I pick up online.

Currently I'm having an issue where my character keeps jumping when the jump key is pressed, but I only wanted him to jump once per press. I looked around online and heard about the if (Input.GetButtonDown("Jump")); line of code which should fix it. I tried implementing it but it doesn't seem to work. I'm guessing my lack of experience with coding format is letting me down, how exactly do I correctly incorporate this into the code below? Simply adding it under void update did nothing, is something else contradicting it? I don't get any errors...it just doesn't work.

I've put my entire script without the attempt to add this line of code below. Any assistance would be much appreciated. Thank you very much and apologies for the simple question!

 public class PlayerController : MonoBehaviour {
 
     // movement variables
     public float maxSpeed; 
 
     // Jumping Variables
     private bool grounded = false; 
     private float groundCheckRadius = 0.2f; 
     public LayerMask groundLayer; 
     public Transform groundCheck; 
     public float jumpHeight;
 
     Rigidbody2D myRB;
     Animator myAnim;
 
     // Use this for initialization
     void Start () {
         myRB = GetComponent<Rigidbody2D> ();
         myAnim = GetComponent<Animator> ();
 
     }
     
     // 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);
 
         myAnim.SetFloat ("direction", Input.GetAxisRaw ("Horizontal"));
 
     }
 }
 
 
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JincSoft · Mar 20, 2016 at 06:21 AM

You will need to check to see if "Jump" is an axis or a button assigned under Edit>Project Settings>Input. By default it's a button so you could try Input.GetButtonDown("Jump"). If you want a testing only usage you can try Input.GetKeyDown(KeyCode.Space) but it is generally not a good idea to hard code inputs like that. If neither of those work (especially the KeyCode.Space) then make sure that grounded is being set to true.

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 Kriffix · Mar 20, 2016 at 11:09 AM

Thanks for your reply JincSoft. I don't seem to be having much luck. I think my lack of fundamental scripting knowledge is most likely letting me down. I'll keep fiddling with it for now. Thanks again!

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

55 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

Related Questions

Is it possible to call OnTrigerCollider2D() without binding any script to the Collider2D directly? 1 Answer

When I change a bool in a script on one object it doesn't change the bool on the same script on another object? 1 Answer

How to make a GetKeyUp action to wait for a few seconds before activating 1 Answer

Move to next animation with clicking the game object 0 Answers

Why is not it casting any Ray? (THIS SHOULD BE EASY) 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