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 beremaran · Jan 16, 2016 at 09:07 PM · c#2d2d-platformer2d-physics2d-gameplay

2D Weird Jumping

Hello,

I'm making a 2D platformer with Unity 5.3. I've created a simple jumping code that supports single jumps but there's a weird action; when player collide with platform from side player launches itself to space! Before code, here's my collision boxes: Collision Boxes I think a single box collider mixing up things, so I changed my collision boxes in different GameObjects. But anything doesn't change.

Here's the jumping code:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public float speed = 25;
     public float jumpSpeed = 1;
 
     bool isGrounded = false;
     
     // Update is called once per frame
     void Update () {
     
         if((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.Space)) && isGrounded)
         {
             rigidBody.AddForce(new Vector2(0, jumpSpeed * 100));
             isGrounded = false;
         }
 
         if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
         {
             if(isGrounded)
             {
                 //changeState(STATE_WALK);
             }
         } else
         {
             if(isGrounded)
             {
                 //changeState(STATE_IDLE);
             }
         }
 
         Vector3 newPos = new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0);
         transform.position += newPos;
 
     }
 
     void OnCollisionEnter2D(Collision2D coll)
     {
         if (coll.collider.tag == "Platform") {
             isGrounded = true;
         }
     }
 
 }
 

Also a video: https://www.youtube.com/watch?v=v2O65If_Vvc&feature=youtu.be

I can't figure out this. Thanks for your help, in advance!

oops.png (12.4 kB)
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
1
Best Answer

Answer by taylank · Jan 16, 2016 at 11:12 PM

The problem is GetKey, which returns true while you hold the key down. When you are on top of the platform that's not creating a problem because as soon as you jump isGrounded returns false in the next frame and prevents further force from being applied. Not so lucky when you hit the platform from the side, because now isGrounded returns true multiple frames, applying the jump force multiple times over.

The solution: - I'd recommend switching to GetKeyDown which returns true only in the first frame a key is pressed. This is what you want if you want single jumps with fixed height. You'll have to use a different mechanism for sustained jumps (where height is based on how long you keep the key pressed). - Instead of setting isGrounded value with collision events, you'd be better off with a downward raycast. This way you will avoid isGrounded returning true when you hit platforms from the side. I believe Unity platformer tutorial explains how to set this up.

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 beremaran · Jan 16, 2016 at 11:43 PM 0
Share

Thank you!

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

76 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

Related Questions

Distance Joint 2D with Kinematic Player 2 Answers

2d platformer physics 0 Answers

Player Not Moving With Platform 2D 1 Answer

2d supporting characters 0 Answers

Walking around 2D planets? 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