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 $$anonymous$$ · Dec 16, 2013 at 12:56 AM · 2draycastjumpmovement script2d platformer

2D Raycast not working as intended

Huy guise, I'm in the process of teaching myself Unity (2D), and I have a bit of a problem... I only have two script files, all attached to the player gameobject sprite thing. One of them is a left and right movement script, and the other one allows jumping. The jump script is the problematic one:

 public class Jump : MonoBehaviour 
 {
     public bool YN = false;
     public float jmppwr = 10f;
 
     void Update()
     {
         RaycastHit2D ground = Physics2D.Raycast(transform.position, -Vector2.up, 1);
         if (ground != null) 
         {
             YN = true;
         }
     }
 
         void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.Space) && YN)
         {
             rigidbody2D.AddForce(new Vector2(0f, jmppwr));
             YN = false;
         }
     }
 }

The issue is that bool YN is always true (even when there's no ground collision object underneath), hence allowing the player to jump mid-air and stuff. Anyone know what the problem is?

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 robertbu · Dec 16, 2013 at 01:11 AM

One problem is that you never set ground to false. Once YN is set to true, it stays true even if the ground is no longer there. It only get set to false after a jump is started. A quick fix using your current structure would be:

  if (ground != null) 
    {
      YN = true;
    }
 else {
    YN = false;
 }

But that is equivalent to:

 YN = (ground != null);
Comment
Add comment · Show 3 · 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 $$anonymous$$ · Dec 16, 2013 at 01:54 AM 0
Share

This is what I got. it still doesn't work, so I probably did something wrong...

 public class Jump : $$anonymous$$onoBehaviour 
 {
     public bool ground = false;
     public bool YN = false;
     public float jmppwr = 10f;
     
     void Update()
     {
         ground = Physics2D.Raycast(transform.position, -Vector2.up, 1);
         YN = ground != null;
     }
     
     void FixedUpdate()
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && YN)
         {
             rigidbody2D.AddForce(new Vector2(0f, jmppwr));
             ground = false;
             YN = false;
         }
     }
 }
avatar image robertbu · Dec 16, 2013 at 02:25 AM 0
Share

Well, it appears that Physics2D.Raycast() does not work the way they indicate in the reference or there is something else going on that I don't understand. I ran a test with your code, and I got the same behavior that you did. I can give you a work around:

 if (ground == null || ground.collider == null)
     YN = false;
 else
     YN = true;

You may be able to just do:

 YN = ground.collider != null;

But I'm reluctant to go with this since a null 'ground' would produce an access violation.

avatar image $$anonymous$$ · Dec 16, 2013 at 02:47 AM 0
Share

EDIT: When I turned off the 2d collider on the player itself, 'ground' behaved as it should. I'm guessing I need an empty...

avatar image
0

Answer by $$anonymous$$ · Dec 16, 2013 at 03:56 AM

Here's final working script in case somebody in the future might need it

 public class Jump : MonoBehaviour 
 {
     public GameObject groundCheck;
     public bool ground = false;
     public float jmppwr = 10f;
     public float rayl = 1f;
     void Awake()
     {
         groundCheck = GameObject.Find("groundCheck");
     }
 
     void Update()
     {
         ground = Physics2D.Raycast(groundCheck.transform.position, -Vector2.up, rayl);
     }
     
     void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.W) && ground)
         {
             rigidbody2D.AddForce(new Vector2(0f, jmppwr));
         }
     }
 }
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

15 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

Related Questions

How to determine if the player can jump, without using raycasts. (2D) 1 Answer

Using RayCasting to check for floor and ceiling? 0 Answers

issues with jumping and linecast2d and raycast2d 0 Answers

[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers

Test line renderer collision? 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