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 Jovy83 · Dec 10, 2015 at 07:13 AM · unity 52draycast2d-physics

Unity Raycast2D results are not as expected?

I'm currently working on a practice project which involves stacking of blocks. It is sort of similar to the game of stackers (the arcade game), except my version uses free-form movement instead of grid-based movement.

I have 3 prefabs for the blocks: SingleBlock, DoubleBlock, TripleBlock.

I structured each of them like this: the parent is an empty gameobject with my MovementScript that moves them left/right, while the children are the block sprite with a BoxCollider2D.

The MovementScript is attached to the empty game object(the parent) so that the block set moves uniformly left/right, for what it's worth.

For the actual stacking logic, I'm using Raycast2D to detect if there is a block below. But the problem is the results I get is unexpected.

Here is a snippet of my current code:

 foreach(Transform t in currentBlockSet.transform)
             {
                 // get all the children of this blockset. to do this, we use the transform beause it is IEnumerable
                 GameObject block = t.gameObject;
                 RaycastHit2D hit = Physics2D.Raycast(block.transform.position, Vector3.down, rayCastLength); // 0.5f raycast length
                 //Debug.DrawRay(block.transform.position, Vector3.down * rayCastLength);
                 if(hit.collider != null)
                 {
                     // this means there is a block below, we hit something
                     Debug.Log("True");
 
 
                 }
                 else
                 {
                     Debug.Log("False");
                 }
 
             }

This code is called each time the player stops the current blockset that is moving by the way.

The problem is I always get true in my logs even though I purposely didn't align the block set properly. I never get false even if I'm way off with my alignment. Why is this so?

I do like to mention that there's nothing else in the scene. It is just the blocks, so there can't be another object to collide with.

Is there something wrong with the logic or how I'm using Raycast2D?

Appreciate any help.

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

1 Reply

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

Answer by GiyomuGames · Dec 10, 2015 at 07:26 AM

Your raycast is hitting the collider of the blockset from which you are casting no? Anyway you should definitely check which object is hit to understand more. You can also draw the ray as a gizmo to see what's happening in the editor.

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 Jovy83 · Dec 10, 2015 at 06:12 PM 0
Share

Thanks for the reply. So I have tried debugging with the collider.tag and collider.gameObject.name as you suggested and I can confirm that the block/raycast is colliding with its own collider. Hence, I always get true in my log.

But how do I make sure that it doesn't collide with itself and actually collide with the block set below? Do you know a viable solution for this? One way I can think of is to create an empty object just below the block's collider and make that the origin of the raycast. Is there a better way by any chance?

avatar image GiyomuGames Jovy83 · Dec 11, 2015 at 05:08 AM 0
Share

You can "manually" set the cast position outside of the collider when you call Physics2D.Raycast. You can calculate it using the collider position and bounds I believe. It's probably something like collider.transform.position - Vector3.up * collider.bounds.y / 2. Not sure, need to test it ^^

avatar image Jovy83 GiyomuGames · Dec 11, 2015 at 05:58 AM 0
Share

Thanks I got the idea already and got it working the way it's supposed to.

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

55 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

Related Questions

Raycasting not working? 1 Answer

Sprites not recognized as whole-spinning in different directions 0 Answers

Linecast, rotate end point relative to objects rotation 1 Answer

Use 2D Effectors with Raycast Collision Method 0 Answers

Gravity Direction Change 2D 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