Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Stubbs89 · Apr 28, 2014 at 05:15 PM · c#2djumpinggrounded

C#: Jump function, grounded stays True

Sorry if I'm re-posting an issue or doing something wrong, this is the first time I'm asking a question here. I'm fairly new to C# and Unity, so I've followed a tutorial in order to get my character controls in order. As far as I can tell, I've done as shown in the tutorial: 2D Character Controllers

When my Player drops down and hits the Ground, the grounded variable stays True. When I press Space to jump, the jump sound is played, so the Jump if-statement is run, but the Player object does not jump, at least not as far as I can see in the game. I increased the float variable for jumpForce to 700 to see if it simply needed more force, as I've set the gravity in the game to -30 as the tutorial said... Moving from side to side works, but the jump isn't. And the grounded variable stays True, but I can still press the Space and hear the jump sound every time I press it.

I've tried looking at another post about this issue, but it hasn't helped me: can't jump in 2d? Please tell me if there is something blatantly obvious that my untrained eyes have overlooked! :)

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour
 {
 
     public float maxSpeed = 10.0f;
     public float jumpForce = 700.0f;
     private bool grounded = false;            // Whether or not the player is grounded.
     public Transform groundCheck;            // A position marking where to check if the player is grounded.
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
 
     float minValX = -6.5f;
     float maxValX = 6.5f;
 
     public AudioClip Jump1;
     
 
     void FixedUpdate (){
 
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 
         Debug.Log (grounded);
 
 
         float move = Input.GetAxis ("Horizontal");
 
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
         if (rigidbody2D.transform.position.x < minValX)
             rigidbody2D.transform.position = new Vector3 (minValX, transform.position.y, transform.position.z);
         if (rigidbody2D.transform.position.x > maxValX)
             rigidbody2D.transform.position = new Vector3 (maxValX, transform.position.y, transform.position.z);
     
     }
 
     void Update(){
         if(grounded && Input.GetKeyDown(KeyCode.Space)){
             Debug.Log ("Jump!");
             AudioSource.PlayClipAtPoint(Jump1, transform.position);
             grounded = false;
             rigidbody2D.AddForce(new Vector2(0.0f,jumpForce));
         }
     }
 }


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

Answer by wibble82 · Apr 28, 2014 at 05:32 PM

Hi there

Your code looks roughly right. Your grounded variable may be staying true continuously as the Physics2D.OverlapCircle tests if there are any objects in the circle at groundCheck.position, with a layer mask that matches whatIsGround. Hence you'll need to make sure the groundCheck transform is setup correctly (presumably its supposed to be the transform of an object attached to the players feet), and the layer mask needs to match the ground's layer.

The correct layer mask to use would probably be something like:

 //get the layer number of the ground
 int ground_layer_number = LayerMask.NameToLayer("Ground")
 
 //create a 'mask' - a 32 bit value with a bit set for each layer to test. 
 //In this case we only want to check for the ground layer so we use 1 shifted left by ground_layer_number bits
 whatIsGround = 1 << ground_layer_number;

In terms of the force code, it looks sensible (assuming that "Jump" is being printed out at the correct time and you aren't seeing any exceptions). However even a force of 700 may be too low! Forces are scaled by the mass of the object AND the time step. Impulses are a better bet for 1 shot forces like this. Check out my tutorial on forces for a bit more info on that:

http://answers.unity3d.com/questions/659413/need-tutorials-for-rigidbodyaddforce.html

Have you tried a ridiculously high number for the force, like a million million? Just to see if its having an effect?

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 Stubbs89 · Apr 28, 2014 at 06:50 PM 0
Share

Thank you for the quick reply! I checked that I had set the various objects to have the right tags and layer groups. Then I tried changing the value of jumpForce in the script to a million, and since I still didn't see any changes in the scene, I decided to inspect the Player object while running the game. Turns out I had input a value out in the Inspector, which was overriding the value in the script! /facepalm I will bookmark your other explanation of AddForce for future refence. :)

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

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

Character jumping at random heights. What in my code is causing this? 1 Answer

Even / Constant Speed and Jumping 2D 1 Answer

Have a delay after each jump, so user cant spam jump 3 Answers

Making a sphere jump on specific surfaces only 4 Answers

How to check for the ground in Unity 2D for Jumping 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