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 /
avatar image
0
Question by firebird127 · Aug 18, 2018 at 07:03 PM · programmingjumpingforcelayerscapsule collider

When i press Jump, i am allowed to double/triple jump even though i called for a capsule check?

Hey guys, im a little new to Unity so i dont quite understand how the engine works yet. Anyways I am trying to get it to where when the jump button is pressed, an impulse force is added only once to make the player jump until it detects collision with a ground layer via capsule check. This works, unfortunately, if the user continuously hits the jump button, the player object with "jiggle" upward with little mini jumps. I am honestly so confused on why my code doesnt work. I would greatly appreciate any help or advice i can get :)

Here is the code: public class playerJump : MonoBehaviour {

 // Set variables.
 private float jumpVelocity;
 public bool grounded;

 [SerializeField]
 private CapsuleCollider col;
 [SerializeField]
 private Rigidbody rb;
 

 // Pass in variables.
 public void Jump(float _jumpVelocity)
 {
     jumpVelocity = _jumpVelocity;
 }

 // Sets new ground layer.
 public LayerMask ground;

 // Checks to see if the player is touching the ground.

void isGrounded() { if (Physics.CheckCapsule((col.bounds.center), new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.radius, ground)) grounded = true; else grounded = false; }

 private void OnCollisionEnter(Collision collision)
 {
     if (gameObject.layer == ground)
         grounded = true;
 }

 private void OnCollisionExit(Collision collision)
 {
     if (gameObject.layer != ground)
         grounded = false;
 }
 // Jumping function
 void performJump()
 {
     isGrounded();

     if (grounded && Input.GetButtonDown("Jump"))
         rb.AddForce(transform.up * jumpVelocity,ForceMode.Impulse);
 }
   

   


 // Use this for initialization
 void Start () {
     col = GetComponent<CapsuleCollider>();
     rb = GetComponent<Rigidbody>();
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     performJump();
 }

}

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
0
Best Answer

Answer by ignacevau · Aug 18, 2018 at 09:54 PM

  • I would like to mention that your OnCollisionExit method is completely useless since the bool ground gets updated every frame before it's used.

  • You are using OnCollisionEnter / OnCollisionExit as well as Physics.CheckCapsule, you only need one of them.


The easiest way to achieve the result is by using OnCollisionEnter / OnCollisionExit and updating the bool grounded on every collision.

     public float jumpVelocity;
     Rigidbody rb;
     bool grounded;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.CompareTag("Ground"))
         {
             grounded = true;
         }
     }
 
     private void OnCollisionExit(Collision collision)
     {
         if (collision.gameObject.CompareTag("Ground"))
         {
             grounded = false;
         }
     }
 
     void PerformJump()
     {
         rb.AddForce(transform.up * jumpVelocity, ForceMode.Impulse);
     }
 
     private void FixedUpdate()
     {
         if(grounded && Input.GetKeyDown(KeyCode.Space))
         {
             PerformJump();
         }
     }

Make sure to add the "Ground" tag to your ground object


You could also make this work with Physics.CheckCapsule and performance-wise, it would probably be more efficient (in most cases not noticeable though).

Comment
Add comment · Show 2 · 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 firebird127 · Aug 19, 2018 at 10:17 PM 0
Share

@ignacevau Thank you so much, i didnt realize i was still using the onCollisionenter/exit function! I appreciate you! I would like to use Physics.CheckCapsule if at all possible so that i can specify to only jump if the collision occurs on the bottom of my player object. I altered the code to fit your recommendations, unfortunately i have come across another problem. Now my player will not jump at all. Do you think you could explain what is happening to me? :)

avatar image firebird127 · Aug 19, 2018 at 10:17 PM 0
Share

public class playerJump : $$anonymous$$onoBehaviour {

 // Set variables.
 private float jumpVelocity;
 public bool grounded;

 [SerializeField]
 private CapsuleCollider col;
 [SerializeField]
 private Rigidbody rb;
 

 // Pass in variables.
 public void Jump(float _jumpVelocity)
 {
     jumpVelocity = _jumpVelocity;
 }

 // Sets new ground layer.
 public Layer$$anonymous$$ask ground;

 // Checks to see if the player is touching the ground.

 private void isGrounded()
 {
     if (Physics.CheckCapsule((col.bounds.center), new Vector3(col.bounds.center.x, col.bounds.$$anonymous$$.y, col.bounds.center.z), col.radius * 0.9f, ground))
         grounded = false;
 }

 // Jumping function
 void performJump()
 {
     isGrounded();
     if (grounded && Input.GetButtonDown("Jump"))
         rb.AddForce(transform.up * jumpVelocity,Force$$anonymous$$ode.Impulse);
 }
   

   


 // Use this for initialization
 void Start () {
     col = GetComponent<CapsuleCollider>();
     rb = GetComponent<Rigidbody>();
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     performJump();
 }
avatar image
0

Answer by firebird127 · Aug 19, 2018 at 10:19 PM

@ignacevau Nevermind! I overlooked a simple typo mistake! i appreciate all of your help!

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

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

101 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 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

Multiple Cars not working 1 Answer

Jump forward then back initial position 0 Answers

Gravity trouble: Falling slow, jumping fast 1 Answer

how to add force? 2 Answers

Ball rolling across boxes randomly 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