- Home /
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).
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.
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.
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
Your answer
