So this is my first post. As usually I spend hours trying different methods and scanning the web for a solution but I don’t know why I just cant put this together.
What I’m trying to do is when I left click my sprite plays an animation. At a certain point it triggers an event to check if anything was in the circle collider to do damage to. If there is something in the collider that is attackable it calls that component to do damage. Very straight forward.
I know how to do simple on trigger calls but that does not seem to work here as I don’t want it to call when the colliders hit but yet when the mouse is clicked.
I’ve been able to detect if an attackable collider is in my collider using Get Layer Mask but how do I find out which object was in the collider so I can call the get component on that particular object?
Thank you.
Check for the mouse click within the trigger function.
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "YourTagHere")
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Trigger has occurred and we have clicked the mouse button");
}
}
}
You have some conflicting verbiage in what you are asking (or i am probably just misunderstanding). You mention in the 2nd paragraph that you want it to damage anything that is attackable within the collider. But in your last paragraph you mention figuring out what collider it is (meaning if there are multiple that you want to hit but want to hit only a certain one?).
It is really easy if you want to attack all objects within that collider, but not terribly difficult to find a certain one either. If you can explain it a bit further between those 2 scenarios i interpreted, maybe i can help you further.
Okay so I tried the code you provided and that did not work either If I remove the on mouse button down it works but not with it. So let me try to clarify some of this.
What I have: 2D Character with a capsule collider non trigger for rigid body. 5 circle trigger colliders for wall detection and ground detection. And one large circle trigger for attack radius detection.
What I want: Is for the large attack circle to be able to detect anything tagged as interactable within the circle. To call the component on that interactable to do what ever.
What I don’t want: Is for this to be be automatic on trigger enter. I want it to only call the method if the button or in this case the mouse was clicked.
Odd that the code doesnt work, its very basic and ive done it a lot. Maybe switch the MouseButtonDown and the tag checking IF blocks. Or change OnTriggerEnter2D to OnTriggerStay2D.
What you are trying to do should work well with the code i provided. You can then check for a certain component on the other object by doing: if(other.gameObject.GetComponent() != null)
Just checked it with OnTriggerStay2D and it works. TriggerEnter did not so apologies for that earlier part.
So yes I did try ontriggerstay and got it to work but its glitchy like it seems to only works at certain times mainly if the character is moving. There must be a better way. I tried to use the LayerMask.GetMask and that works amazing for detection but I cant find a way to then call the components I’m touching.
There are multiple ways to check for collisions, try OverlapCircleAll
Alright well I got it to work I think, for now at least. For anyone wondering what madness was needed for future references. Here you go.
CircleCollider2D circleCollider;
private void Start()
{
circleCollider = GetComponent<CircleCollider2D>();
}
public void AttackEvent()
{
int numOfColliders = 5;
Collider2D[] colliders = new Collider2D[numOfColliders];
ContactFilter2D filter = new ContactFilter2D();
filter.SetLayerMask(LayerMask.GetMask("Attackable"));
circleCollider.OverlapCollider(filter, colliders);
foreach (Collider2D collider in colliders)
{
print(collider);
}
}
-
You set an array size like 5 for example saying you only want the first 5 results. I feel like this should be irrelevant I just can’t seem to figure out how to make it figure its self out instead of specifying the number. If you only want 1 result make it 1.
-
You set a filter. In my case I wanted only the objects that had Attackable as its layer mask.
-
Run the filter by setting the collider you are checking within.
-
Run a foreach over the results and call the method you want.
1 Like
Thanks vincewa, that’s exactly what I was looking for! About array size, you can use List instead of array:
var colliders = new List<Collider2D>();
This way you will as many results as there are colliders in reach
OnTriggerEnter2D did not work because you would need to be pressing the mouse button on the exact same frame as the OnTriggerEnter2d was triggered.