• 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
Question by MarkPwns1 · Aug 27, 2014 at 02:40 AM · 2dmovementplayerjumpplatformer

Why do I double jump? This isn't supposed to happen...

So I have this script, shown below: var moveSpeed : float = 0.1; private var IsGrounded = false; var JumpStrength : float = 5; private var animator;

 function Start() {
     animator = GetComponent("Animator");
 }
 
 function Update () {
     gameObject.transform.Translate(Input.GetAxis("Horizontal") * moveSpeed, 0, 0);
 
 
     if(Input.GetKey("a")) {
         gameObject.transform.localScale.x = -1;
     }else if( Input.GetKey("d") ){
         gameObject.transform.localScale.x = 1;
     }
 
     if(Input.GetKey("a") || Input.GetKey("d")) {
         animator.SetBool("running", true);
         
     }else{
         animator.SetBool("running", false);
     }
 
     if(Input.GetKeyDown("space") && IsGrounded) {
         IsGrounded = false;
         Jump();
     }
 }
 
 function OnCollisionEnter2D(col : Collision2D) {
     if(col.gameObject.tag == "Ground")
         IsGrounded = true;
 }
 
 function Jump() {
     IsGrounded = false;
     gameObject.rigidbody2D.AddForce(Vector2.up * JumpStrength);
     IsGrounded = false;
 }

and whenever I hold either the A or D keys, and press space, I have the ability to double jump, which is not intended and is rather overpowered to add to my game. I assume there's something wrong with gameObject.transform.Translate(Input.GetAxis("Horizontal") * moveSpeed, 0, 0); that doesn't like collisions. Is there any workaround or solution?

Comment

People who like this

0 Show 1
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 jokim · Aug 27, 2014 at 02:40 PM 0
Share

Mmhhh, I'm not sure using translate is the way to go indeed...

Can you try working this out using add force instead ?

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by MarkPwns1 · Aug 27, 2014 at 02:40 PM

Finally! I've managed to fix it. The problem was exactly what you said, but when adding the OnCollisionExit2D didn't work, I just decided a more simple answer would be to just add a yield WaitForSeconds(0.1); to the end of my Jump() method. Thank you anyway for your help!

Comment

People who like this

0 Show 0 · 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

Answer by kacyesp · Aug 27, 2014 at 03:45 AM

I think the problem might be that right after you set IsGrounded to false in your Jump() function, OnCollisionEnter2D sets the boolean back to true because you the ground is still colliding with your collider as you're taking off.

Can you try adding an OnCollisionExit2D function with the same code as your OnCollisionEnter2D function but instead of setting grounded to true, set it to false ?

Comment

People who like this

0 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 MarkPwns1 · Aug 27, 2014 at 02:35 PM 0
Share

Hmm, I did exactly as you said, but with no luck. Now the player can jump infinitely... All I really want is some 2D character controller on the asset store. Anyway, like I said, I assume it's a problem with line 4 not liking collision.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2d Platformer Jumping Not Working 1 Answer

My player doesn't jump when he is in a platform which is growing. 1 Answer

2D Platformer Ai Movement Decision Making Problems 0 Answers

How to make my character stop stuttering when walking down a slope? 2 Answers

Help with player script. 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