Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Mike_El · May 30, 2018 at 11:54 AM · raycastcollider2dlinecast

Linecast detects gameObject underneath the collided one

Ive been looking over my code and using other questions answers and using the documentation to figure this one out but I can't find anything. Basically I got a few rows on objects stacked on top of each other and I am using linecast to detect and destroy them when the player runs into them. I've tried just isolating the collided game object into a gameobject variable, tried making the box collider smaller. Nothing seems to work

This is what happens (Keep an eye on the 2 highlighted object names in the Hierarchy):
https://youtu.be/JPA82gzD3Ts

I've been using various stages of this particular code, this is the latest iteration:

 void OnCollisionEnter2D(Collision2D collEnter)
     {    
         if (Input.GetKey ("a") && checkLeft == true && checkBtm == false) {
             countDown -= Time.deltaTime;
             print (countDown);
             collEnter.gameObject.name = "Test";
             spr = collEnter.gameObject.GetComponent<SpriteRenderer> ().sprite;
             if (countDown <= 0) {
                 print (spr);
                 Destroy (collEnter.gameObject);
             }
         }
     }

I should note that checkLeft and checkBtm relate to the 2 of the 3 Linecasts I have assigned to the player and this is currently placed in a OnCollisionEnter2D.

I have worked out that the player seems to somehow first collide with the object underneath the object I am looking to destroy, I found this out by using print(collEnter.collider);.

I'd love some feedback on how to fix this issue as I am completely stuck. I originally had this fixed by using a bunch of tags and y lvl iterations in my code but that got to be a pain in the ass for assigning new tags and it would have gotten even worse as I wasn't even a 3rd of the way through with that.

EDIT: Additional information as requested. Linecast is implemented in the Update function like this checkLeft = Physics2D.Linecast (leftStart.position, leftEnd.position);.

I havent get up a Raycast (but I will do that now, I thought I originally did. So that might just fix all my issues, I'll edit this again if that proves to be true) and The body of the player is is on Layer 2 while the GameObjects I am Destroying are on Layer 1 (Honestly didn't consider this an issue).

Comment
Add comment · Show 2
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 Captain_Pineapple · May 30, 2018 at 02:25 PM 0
Share

Hey there,

can post a few more specifics regarding your problem? - Where and how did you implement your raycasts? - Do you use OnCollisionEnter? - Why the one or the other? - Do you use layers for your project?

This might help narrowing down what is happening.

avatar image Mike_El Captain_Pineapple · May 30, 2018 at 03:39 PM 0
Share

I have linked a Youtube video showcasing the problem. In the video you will see the 2 highlighted names change to Test, when Destroy() is implemented, it causes both of these objects to be destroyed when I only want the object I am on the same lvl with destroyed. As I posted in the question, the code I put in is held inside of a OnCollisionEnter2D but for clarification I will edit that part in as well as the other information you asked for.

1 Reply

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

Answer by Mike_El · May 31, 2018 at 04:34 AM

I was able to solve my own issue by swapping Box Colliders for Polygon Colliders and slanting the edges of the collider. For some reason the RigidBody or Box Collider on my player interacted with the Box Collider below the intended Object. Slanting the edges with Polygon Colliders stopped this issue

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

123 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

Related Questions

Raycast Does not Detect Collider 2 Answers

issues with jumping and linecast2d and raycast2d 0 Answers

Pointcast? - Raycast Point X from same Point X 0 Answers

why Raycast2D doesn't work properly? 0 Answers

Raycast or Linecast to existing Raycast hit.point, to check Line of Sight 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