• 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
1
Question by Ryanmstc · Apr 05, 2015 at 10:54 PM · c#jumpyaxis

I can only jump once on my jump code C#

In my jump script there is a statement that says, if not grounded and moving along the y axis, grounded = true however, this statement doesn't seem to be active.

 using UnityEngine;
 using System.Collections;
 
 public class Jump : MonoBehaviour {
     public bool grounded = true;
     public float jumpPower = 50000;
     // Use this for initialization
     void Start () {
         grounded = true;
     }
 
     void Update () {
         if(!grounded && GetComponent<Rigidbody2D>().velocity.y == 0) {
             grounded = true;
         }
         if (Input.GetButtonDown("Jump") && grounded == true) {
             GetComponent<Rigidbody2D>().AddForce(transform.up*jumpPower);
             grounded = false;
         }
     }
 }

Is there something wrong within my script? The problem is that I can only jump once.

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
1

Answer by Oribow · Apr 05, 2015 at 11:06 PM

Better use raycast instead of checking the velocity. Its more accurate.

   If(Physics.Raycast(transform.position-transform.localscale/2, Vectore3.Down, 0.1f)
 //IsGrounded = 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 Keearix · Apr 06, 2015 at 02:16 PM

You can use the method called OnCollisionEnter2D.

As long as your ground has a tag set to Ground, which can be done at the top of the inspector, this script works.

Here is an example jump script that works perfectly :

using UnityEngine; using System.Collections;

public class playerMovement : MonoBehaviour {

 public float jumpSpeed = 50f;
 public bool grounded = false;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
         if (grounded) {
             if (Input.GetKey (KeyCode.Space)) {
                 GetComponent<Rigidbody2D> ().AddForce (Vector2.up * jumpSpeed);
                 grounded = false;
             }
         }
     }

 void OnCollisionEnter2D (Collision2D other){
     if (other.transform.tag == "Ground") {
         grounded = true;
     }
 }

}

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 _Gkxd · Apr 06, 2015 at 02:28 PM 0
Share

This code assumes that whenever you press the jump key, you exit the collision with the ground, which isn't always true in all games. For example, if you jump but are blocked by a low ceiling, you might not exit the collision with the ground, which means that OnCollisionEnter() method would never trigger. You won't be able to jump until you triggered OnCollisionEnter() some other way.

For some games, this can be a problem, and raycasting would solve that.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2D C# Jump Script Addforce 2 Answers

Jump/grounding bugs, advice appreciated 1 Answer

jump with character controller best way? 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