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 ezhilartworks · Mar 25, 2016 at 04:17 PM · jump

simple 2d jump does not work?

the below script is written for a simple jump when the player's 2d collider hits the grounds collider it works for sometime but after some iterations on continuously hitting the space the player remains static why is that so ? is raycast the only way we can do this?

public class jumper : MonoBehaviour { public float Jumpheight; private bool isJumping=false;

 // Use this for initialization
 void Start () {
 
 }

 // Update is called once per frame
 void Update () {

     if (Input.GetKeyDown(KeyCode.Space) && isJumping == false) {

         GetComponent<Rigidbody2D> ().AddForce (new Vector2(0,  Jumpheight));
         isJumping = true;
         }

     }

 void OnCollisionEnter2D (Collision2D col)
 {
     if (col.transform.tag == "ground")
     {
         isJumping = false;
     }
 }

}

thanks in advance :)

Comment
Add comment · Show 12
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 Wolfshadow · Mar 25, 2016 at 04:44 PM 0
Share

you do define isJumping above, right?

avatar image ezhilartworks Wolfshadow · Mar 26, 2016 at 06:07 AM 0
Share

yes i have added it

avatar image Wolfshadow · Mar 25, 2016 at 04:46 PM 0
Share

my bad, you do have it

avatar image ezhilartworks Wolfshadow · Mar 26, 2016 at 06:07 AM 0
Share

it is alright :)

avatar image ComradeVanti · Mar 25, 2016 at 05:29 PM 0
Share
  1. Where does the player get stuck? Air or Ground?

  2. Can you still move him sideways if that is an option.

  3. What does a Debug.Log of isJumping in Update say?

  4. Does OnCollisionEnter2D get called correctly? Check with a Debug.

After you have answered these questions we will look further :)

avatar image ezhilartworks ComradeVanti · Mar 26, 2016 at 06:12 AM 0
Share

1> the player gets stuck on colliding with the ground

2>nope it is an infinite runner with scrolling background with no sideway movement

3>4> i runned debug it works fine for some time but it the OnCollisionEnter2D does not get called after certain iterations

i will send the screenshot

avatar image ezhilartworks ComradeVanti · Mar 26, 2016 at 06:13 AM 0
Share

alt text

jumper-debug.png (250.1 kB)
avatar image ezhilartworks ComradeVanti · Mar 26, 2016 at 06:14 AM 0
Share

the script for the above is

using System.Collections;

public class jumper : $$anonymous$$onoBehaviour { public float Jumpheight; private bool isJumping=false;

 // Use this for initialization
 void Start () {
 
 }

 // Update is called once per frame
 void Update () {

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && isJumping == false) {
         Debug.Log (1);

         GetComponent<Rigidbody2D> ().AddForce (new Vector2(0,  Jumpheight));
         isJumping = true;
         Debug.Log (2);
         }

     }

 void OnCollisionEnter2D (Collision2D col)
 {
     Debug.Log (3);
     if (col.transform.tag == "ground")
         Debug.Log (4);
     {
         isJumping = false;
         Debug.Log (5);
     }
 }

}

avatar image TreyH · Mar 25, 2016 at 05:59 PM 0
Share

You're going to have trouble doing things that way.

1- You never check to see if the player left the ground, but your jump flag is only reset when they contact ground again. 2- http://docs.unity3d.com/ScriptReference/Force$$anonymous$$ode2D.Impulse.html

Consider using a bool to prevent multiple jump attempts, but you will want some way of resetting this in the event your player never leaves the ground at all.

avatar image ezhilartworks TreyH · Mar 26, 2016 at 06:20 AM 0
Share

So i need to check if player leaves the ground too? but the collider2d is not called at all after sometime even if i check if player leaves the ground or not would i be able to use collider2d? anyways i used raycast to solve my problem but i was just wondering why it cannot be done using collider2d although it works for sometime but why would it not call the OnCollision2D after few iterations is what i am confused about

avatar image ezhilartworks · Mar 26, 2016 at 06:28 AM 0
Share

first all thank you guys for such quick replies and support :)

for those who are wondering what could be the solution for this the below is the given script you can have double triple and as much as jump you want by using this code cheers happy coding :)

using UnityEngine; using System.Collections;

public class jumpdude : $$anonymous$$onoBehaviour {

 public float jumpForce = 0f;
 private Rigidbody2D rbody2D;
 public Layer$$anonymous$$ask whatIsGround;
 public bool grounded = false;
 public GameObject groundCheckObject;
 public float groundCheckDistance = 0f;
 public bool can$$anonymous$$ultipleJump = false;
 private int jumpCounter = 0;
 public int maxJumps = 0;




 // Use this for initialization
 void Start () {
     rbody2D = GetComponent<Rigidbody2D>();
     jumpCounter = 1;
 
 }
 
 // Update is called once per frame
 void Update () {
     RaycastHit2D hit01 = Physics2D.Raycast (
                              groundCheckObject.transform.position,
                              Vector2.down,
                              groundCheckDistance,
                              whatIsGround);

     grounded = (hit01.collider != null);

     if (can$$anonymous$$ultipleJump) {
         $$anonymous$$ultipleJump ();
     } else {
         singleJump ();
     }

 }
 void singleJump()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && grounded) {
         rbody2D.AddForce(Vector2.up * jumpForce);

     }
 }
 void $$anonymous$$ultipleJump()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){
         if (jumpCounter < maxJumps) {
             rbody2D.AddForce (Vector2.up * jumpForce);
             jumpCounter++;
         }
     }
     if(grounded)
     {
         jumpCounter = 1;
     }
 }

}

avatar image ezhilartworks ezhilartworks · Mar 26, 2016 at 06:31 AM 0
Share

put the ground in the layer ground and number of max jump in the max jump tab and uncheck the max jump if you want single jump create a empty object as a child to the player and add it in the ground check object

happy coding cheers

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.

Follow this Question

Answers Answers and Comments

53 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

Related Questions

Jump raycast not working 0 Answers

Jump problem 1 Answer

I Need jump only one time : ( 0 Answers

Double jump. Jumping after falling off a platform 1 Answer

I'm trying to get the character to jump, is it because the trigger key hasn't been inputed correctly? 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