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 Hanarchist · Jul 03, 2014 at 04:08 PM · raycastcolliderphysics2d

Physics2D.Raycast.collider is always returning an object?

Hey, guys!

So I've ran into a problem. I'm trying to make it to where a player cannot infinitely jump in the air, so I've ran through some tutorials on how to achieve this, and this is what I have so far.

     void Jump() {
         if (isGrounded.collider == null) {
             return;
         }
         rigidbody2D.AddForce (new Vector2(0, jumpHeight), ForceMode2D.Force);
     }
 
     void FixedUpdate() {
         isGrounded = Physics2D.Raycast (transform.position, -Vector2.up, .5f);
         Debug.Log (isGrounded);
         //Debug.Log (transform.position);
     }

The function Jump() is called from the Update function. So basically what I've done is check whether isGrounded.collider is null, for that would mean I'm in the air (or that's what I think that's what it's achieving). If that's the case, simply return nothing without actually jumping. However, when I'm actually in the game, isGrounded.collider is always returning something, regardless of whether I'm actually grounded or not. I've tried everything (lowering distance, using different functions, etc.), but nothing's working. Please help!

Or is there a better way to achieve what I'm going for?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hanarchist · Jul 03, 2014 at 05:40 PM

Okay, I found a solution. Instead of using Physics2D, I chose to go with Physics instead, and things seem to be working now (changed code accordingly, and with appropriate Box Colliders and Rigidbodies).

Thanks for all the people who took the time to 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
avatar image
0

Answer by tanoshimi · Jul 03, 2014 at 04:13 PM

Instead of:

 isGrounded = Physics2D.Raycast (transform.position, -Vector2.up, .5f);
 Debug.Log (isGrounded);

Try:

 RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, .5f);
 if (hit.collider != null) {
     Debug.Log (isGrounded);
 }
Comment
Add comment · Show 4 · 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 Hanarchist · Jul 03, 2014 at 04:29 PM 0
Share

Thanks for the help! This method didn't quite help, and I still achieve the effect of infinite jumping. isGrounded is still showing up to be true for all cases.

avatar image tanoshimi · Jul 03, 2014 at 04:38 PM 0
Share

What's the scale of your game world? Try setting .5f to be 0.001f ins$$anonymous$$d.

avatar image Hanarchist · Jul 03, 2014 at 04:42 PM 0
Share

I don't think the distance is the issue. I've already tried setting the distance to be (something like) 0.00000000000000001f, and it still read to be true.

Jumping off the side to where I ideally would not be touching any collider still shows isGrounded to be true.

Am I misunderstanding something about Physics2D.Raycast?

avatar image Hanarchist · Jul 03, 2014 at 04:45 PM 0
Share

By the way, am I correct in interpreting your

 if (hit.collider != null) {
     Debug.Log (isGrounded);
 }

to actually meaning

 if (hit.collider != null) {
     isGrounded = true;
 }

?

avatar image
0

Answer by robertbu · Jul 03, 2014 at 04:13 PM

You want to check the collider or the transform:

  isGrounded = (Physics2D.Raycast (transform.position, -Vector2.up, .5f).collider != null);

The RaycastHit2D returned by Physics2D is a struct and will never be null.

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 Hanarchist · Jul 03, 2014 at 04:28 PM 0
Share

Thanks for the answer! I've tried this method, and changed everything accordingly, but it's still showing isGrounded to be true for all cases.

avatar image
0

Answer by oct · Jul 04, 2014 at 11:20 AM

check to see if the ray from transform.position is not hitting some child (or something) of the object itself. use Debug.DrawLine to visualy see the ray

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

22 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

Related Questions

Raycast 2D hits sprite with collider, but returns error when not hitting any collider 2 Answers

How to rotate a player around the corner of a box? 0 Answers

Applying damage with Flamethrower-like weapon 1 Answer

Can't get a laser working properly. 2 Answers

How can I make diagonal collisions work with raycasts from a boxcollider? (3D, C#) 0 Answers


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