Hello everyone, I am relatively new to the unity scene here, although I do have plenty of coding and game dev (SourceSDK) experience. I am having an issue that I am not able to figure out.
I have been through the documentation, and plenty of tutorials, and I keep coming back to the same problem. When my main player tagged “Player” (Blue) collides with “Destroyable” (red) the destroyable should be destroyed.
My setup. I am working in 2D mobile.
I have a completely blank scene, with two identical sprites.
- My goal is to make Blue kill red. Simple enough.
Basic Setup:
In game-mode, the sprites have registered collision and have even moved.
The image below is in-game, with default background. Contact has been made.
Contact being registered.
The next two images I posted as thumbnails as they are long images.
The “Player” tag (the blue one):
The “Destroyable” tag (the red one):
Notice the Player layer and Enemy layer.
- In the Edit> Project settings > physics 2d > Collision layer matrix, I did enable Enemy and Player.
I have tried the appropriate combination (I believe) from research prior to this post. It is my understanding that if I want to use is Trigger, then I must use the OnTriggerEnter2D method (I am in 2D), with 2D colliders. If Is Trigger is OFF, then I use OnCollisionEnter2D. I am tried both set-ups with similar results, except, that if isTrigger is enabled, then that object no longer bounces away at collision and registers nothing in the collisions - ( is this the where I should be looking? )
So my setup has:
Is Trigger OFF and the following:
using UnityEngine;
public class CollisionDestroy : MonoBehaviour
{
// This method is called when a collision occurs with another collider
private void OnCollisionEnter2D(Collision2D collision)
{
// Check if the collided object has the tag "Destroyable"
if (collision.gameObject.CompareTag("Destroyable"))
{
// Destroy the collided object
Destroy(collision.gameObject);
}
}
}
Did not work. - Objects bounce
If my setup has:
Is Trigger ON and the following:
using UnityEngine;
public class TriggerDestroy : MonoBehaviour
{
// This method is called when another collider enters the trigger collider attached to this object
private void OnTriggerEnter2D(Collider2D other)
{
// Check if the other collider has the tag "Destroyable"
if (other.CompareTag("Destroyable"))
{
// Destroy the other object
Destroy(other.gameObject);
}
}
}
- Objects bounce
Just to keep attempting research prior to posting, also tried both combinations of the above Trig or Collision with the method below to check if it was a hierarchy related issue.
using UnityEngine;
using UnityEngine;
public class CollisionDestroyer : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Collision detected with: " + collision.gameObject.name);
// Accessing the Transform of the collided game object
Transform collidedTransform = collision.transform;
// Loop through all child objects of the collided game object
for (int i = collidedTransform.childCount - 1; i >= 0; i--)
{
// Accessing each child game object
GameObject childObject = collidedTransform.GetChild(i).gameObject;
// Check if the child object has the specified tag
if (childObject.CompareTag("Destroyable"))
{
// Destroy the child object
Destroy(childObject);
}
}
}
}
-
Objects bounce
-
I have also dragged the code snippet onto the other object, with the inverted tag logic to see if it was something weird - no.
-
I have tried one Rigidbody, and two Rigidbodies, both 2D and 3D with the appropriate collider2D/collider trigger2D/trigger isTrigger…
-
I have tried both 2D suffix, and without, coupled with the appropriate 2D or non Collider / Rigid Body combo. Each attempt going through the same steps of this post - no.
-
I have increased and decreased the collider size to extend further, or be contained within the sprites, - no.
-
I translated both players to Z=0, and main camera is defaulted at Z=-10.
-
I have moved the objects so they start far, and come in, as well as start overlapping (image above shown are overlapping) - no
-
I have attempted to use gravity / no gravity, as well as simulated masses, etc (this should be irrelevant to the point of what I am trying to do, but again, i’m going through the motions - part of learning yayy) - I should go outside.
As I wrote this post, I attempted each of these again just to confirm i’m human and I thrive on pain? lol
There are no other objects in the scene. I couldn’t figure this out on a bigger project, so I opened a new one to figure it out.
Lastly, in the two thumbnail images, you’ll see sections that are minimized or closed off, ALL settings in there are 0, with no layer overrides, etc. I know I am saying "I have tried, the appropriate…" That should be taken with more than just a grain of salt lol but I swear, from what I am reading, this should be working
insert face O_____O
Apologies on the long post, and any verbiage I may be using incorrectly. Please feel free to teach me a thing-or-two. Anyways, I just want to be thorough as I can see I am not the only one that had this issue, and once it’s solved, I’m hoping this can help.
-JB