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 JoshuaOreskovich · Jun 13, 2020 at 02:01 PM · raycastjumpground

Raycasting not turning off jump. Would really love some help on my code.

Jumping works fine but i can jump mid air, even with this code .. where is the flaw in my code? thank you in advance.

[SerializeField] float throttle = 6f; // pawn speed [SerializeField] Rigidbody2D rb2d; [SerializeField] float jmpfrc = 300f;

 public bool isGrounded = true;

 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
 }
 void Update()
 {
     //Debug.DrawRay(transform.position, new Vector2(0, -1), Color.red, .5f);
     if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
     {
         transform.position += transform.right * throttle * Time.deltaTime * Input.GetAxis("Horizontal");
     }

     // **** IF GROUNDED ***************************************************************
     if (Physics2D.Raycast(transform.position, new Vector2(0, -1), .5f))
     {
         isGrounded = true;
     }
     else
     {
         isGrounded = false;
     }
     // **** END IF GROUNDED ***********************************************************

     // **** JUMP **********************************************************************
     if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
     {
         rb2d.AddForce(transform.up * jmpfrc);
         isGrounded = false;
     }
     // **** END JUMP ********************************************************************
 }
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
Best Answer

Answer by WaqasHaiderDev · Jun 13, 2020 at 02:17 PM

I could understand your code and can checkout why raycasting is not working. But since you said your main aim is to stop mid air jump, I would recommend using vertical velocity check instead of raycasting. Something like this should work

  // **** JUMP **********************************************************************
          if (Input.GetKeyDown(KeyCode.Space) && rb2d.velocity.y == 0)
          {
              rb2d.AddForce(transform.up * jmpfrc);
     
          }
          // **** END JUMP ********************************************************************

also instead of calling physics functions in update, try to always call them in fixed update. Because update function depends on game performance on certain mobile which is measured in FPS so update depends on rendering FPS but fixed update will call after each 0.02 seconds (or whatever is set in physics settings in project settings). So a better modified form should be

 [SerializeField] float throttle = 6f; // pawn speed
  [SerializeField] Rigidbody2D rb2d; 
 [SerializeField] float jmpfrc = 300f;
 
  public bool jump = false;
  void Start()
  {
      rb2d = GetComponent<Rigidbody2D>();
  }
  void Update()
  {
     
      if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
      {
          transform.position += transform.right * throttle * Time.deltaTime * Input.GetAxis("Horizontal");
      }
      
      
    
      // **** JUMP **********************************************************************
      if (Input.GetKeyDown(KeyCode.Space) && rb2d.velocity.y == 0)
      {
         jump = true;
          
      }
      // **** END JUMP ********************************************************************
  }
 
 void FixedUpdate()
 {
 jump = false;
 rb2d.AddForce(transform.up * jmpfrc);
 }
Comment
Add comment · Show 5 · 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 JoshuaOreskovich · Jun 13, 2020 at 02:26 PM 0
Share

I like your answer, thank you kindly for the help. I still would like to know why the raycasting isn't working, it really bugs me.

avatar image WaqasHaiderDev JoshuaOreskovich · Jun 13, 2020 at 03:22 PM 1
Share

Hi, I never worked before with 2D raycasting but I am using 3D raycasting in my project. But however I tried your code and here are outcomes.

Basically your Ray is colliding with your player collider also and hence even if it is in air, it is still detecting a collider. And reporting IsGrounded as true. So what you can do is either use a layer $$anonymous$$ask, or use raycast hit information and check if it is detecting required colliders. Below is the approach.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TRY : $$anonymous$$onoBehaviour
 {
    [SerializeField] float throttle = 6f; // pawn speed
     [SerializeField] Rigidbody2D rb2d;
     [SerializeField] float jmpfrc = 300f;
     private RaycastHit2D raycastHit2D;
 
     public bool isGrounded = true;
 
     // Start is called before the first frame update
     
         
     void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
     void Update()
     {
         Debug.DrawRay(transform.position, new Vector2(0, -20), Color.red, .5f);
         
         if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
         {
             transform.position += transform.right * throttle * Time.deltaTime * Input.GetAxis("Horizontal");
         }
         // **** IF GROUNDED ***************************************************************
         raycastHit2D = Physics2D.Raycast(transform.position, new Vector2(0, -20), 0.5f);
         
         if (raycastHit2D.collider.gameObject.layer == Layer$$anonymous$$ask.NameToLayer("YOUR REQUIRED LAYER NA$$anonymous$$E"))
         {
                        isGrounded = true;
             
         }
         else
         {
             isGrounded = false;
         }
         // **** END IF GROUNDED ***********************************************************
         // **** JU$$anonymous$$P **********************************************************************
         if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
         {
             rb2d.AddForce(transform.up * jmpfrc);
             isGrounded = false;
         }
         // **** END JU$$anonymous$$P ********************************************************************
     }
 
 
 }

avatar image WaqasHaiderDev JoshuaOreskovich · Jun 13, 2020 at 03:25 PM 1
Share

Or alternatively, the approach using layer mask is below

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TRY : $$anonymous$$onoBehaviour
 {
    [SerializeField] float throttle = 6f; // pawn speed
     [SerializeField] Rigidbody2D rb2d;
     [SerializeField] float jmpfrc = 300f;
     public Layer$$anonymous$$ask groundLayer;
 
     public bool isGrounded = true;
 
     // Start is called before the first frame update
     
         
     void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
     void Update()
     {
         Debug.DrawRay(transform.position, new Vector2(0, -20), Color.red, .5f);
         
         if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
         {
             transform.position += transform.right * throttle * Time.deltaTime * Input.GetAxis("Horizontal");
         }
         // **** IF GROUNDED ***************************************************************
        
         
         if (Physics2D.Raycast(transform.position, new Vector2(0, -20), 0.5f,groundLayer))
         {
             
                 isGrounded = true;
             
         }
         else
         {
             isGrounded = false;
         }
         // **** END IF GROUNDED ***********************************************************
         // **** JU$$anonymous$$P **********************************************************************
         if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
         {
             rb2d.AddForce(transform.up * jmpfrc);
             isGrounded = false;
         }
         // **** END JU$$anonymous$$P ********************************************************************
     }
 
 
 }

And adjust the layer in inspector.

Now which approach is most suitable?

The first I told you using vertical velocity of rigid body because it will avoid casting rays in each update call and remembers casting rays are performance heavy. But out of two approaches of Layer mask or raycastHid2D, Layermask is more suitable because, it will only detect layers which will be assigned in layer mask otherwise raycast hit will first detect all layers and then it will detect if those colliders matches your layer.

If your question is answered, don't forget to accept the answer. Regards,

avatar image WaqasHaiderDev JoshuaOreskovich · Jun 13, 2020 at 03:42 PM 1
Share

Also remember, the vertical velocity may be zero two times, once at ground and once at peak point. So it is better to include another check also. something similar

  private void OnCollisionEnter2D(Collision2D collision)
     {
         if(collision.gameObject.layer == Layer$$anonymous$$ask.NameToLayer("YOUR GROUND LAYER NA$$anonymous$$E") && rb2d.velocity.y == 0)
         {
             isGrounded = true;
         }
     }
 
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.layer == Layer$$anonymous$$ask.NameToLayer("YOUR GROUND LAYER NA$$anonymous$$E"))
         {
             isGrounded = false;
         }
     }

And why so? Because this will ensure if player is touching ground while its velocity is zero only then IsGrounded should be true. Otherwise there may be case when player is jumping and its head is touching ground. So Ins$$anonymous$$d of single rigid body velcity check or is grounded check, Use this check.

Show more comments
avatar image
0

Answer by JoshuaOreskovich · Jun 13, 2020 at 08:16 PM

thank you GameHourStudio for all 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

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

RayCast to test if player is grounded 1 Answer

The grounded bool of my jump function doesn't become true, using ray cast 1 Answer

issues with jumping and linecast2d and raycast2d 0 Answers

jump linecast 0 Answers

Raycast problem trough the terrain 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