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 /
This question was closed May 23, 2017 at 06:31 PM by Kuboko for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Kuboko · May 23, 2017 at 06:46 AM · physics2draycasthit2d

Problem with raycast

Hi I have a problem with a raycasting. I wanted to use it to check if player is on ground but ray isn't detecting anything, becuse it doesn't start at the bottom of model but on center of it.

  hit = Physics2D.Raycast(transform.position, -Vector2.down, jumpMargin);
          if (hit.collider != null)
          {
              Debug.LogWarning(hit.collider);
              onGround = true;
              Debug.LogWarning("onGround");
              //if (hit.collider.tag == "Floor" || hit.collider.tag == "Box")
          }
          else
          {
              onGround = false;
              Debug.LogWarning("inAir");
          }



alt text

Comment
Add comment · Show 1
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 SohailBukhari · May 23, 2017 at 06:48 AM 1
Share

Just $$anonymous$$ake Empty GameObject under your player and cast ray from that gameObject.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Piyush_Pandey · May 23, 2017 at 11:00 AM

You are raycasting from the center position. The reason it not detects the floor might be because its not long enough. You can find the bottom most point of you sprite, and then raycast from here.

  private Vector3 bottomCheckPosition;
     private Transform thisTransform;
     void Start()
     {
         thisTransform = transform;
         bottomCheckPosition = GetComponent<BoxCollider2D>().size.y/2;
     }
 
     void Update()
     {
         Vector2 checkPos = new Vector2(thisTransform.position.x , thisTransform.position.y-bottomCheckPosition , thisTransform.position.z)
 
             Ray hit = Physics2D.Raycast(checkPos, -Vector2.down, jumpMargin);
         if (hit.collider != null)
         {
             Debug.LogWarning(hit.collider);
             onGround = true;
             Debug.LogWarning("onGround");
             //if (hit.collider.tag == "Floor" || hit.collider.tag == "Box")
         }
         else
         {
             onGround = false;
             Debug.LogWarning("inAir");
         }
 
     }


   

Also, i would suggest not to use -Vector2.down because its magnitude is just 1. You can use new Vector2(0 , - downwardDistance) where you can control the magnitude of how much it should go in that direction.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast2D not working with LayerMask 1 Answer

I don't get Raycasts 0 Answers

Can RaycastHit2D.point be inside a BoxCollider2D if raycast was performed from outside that collider? 1 Answer

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

Raycasting exclusively on trigger colliders 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