OnMouseDown to many objects
Hi there!
I need possible check all objects that hit to mouseDown. For example
if (Input.GetMouseButtonDown (0)) {
hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition),Vector2.zero);
if(hit.collider != null) {
if (hit.transform.name == "PlayBt") {
Debug.Log ("yeah");
}
}
}
But if something other gameObject with collider place over this, it is not works. How fix this?
Thanks!
You want to be able to hit (raycast) the "PlayBt" even if there is an obstacle (some GameObject with collider) in the way?
Answer by PizzaPie · May 03, 2017 at 12:56 PM
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D[] hits;
hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //not sure about the directions, but if they worked they good for this too
if (hits == null)
return;
for (int i = 0; i < hits.Length; i++)
{
if (hits[i].collider.name == "PlayBt")
{
Debug.Log("yeah!");
}
}
As commented in the code i didn't test the directions so i am not sure if they are right, no 2D project in hand to test it. Keep in mind that the above code has great performance cost. Cheers
Edit: the above method is killing performance so go with this, my bad i didn't mention it before
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, someMask);
if (hit.collider != null)
{
if(hit.collider.name == "PlayBt") //no need for this if no other object has the same LayerMask
{
Debug.Log("yeah!");
}
}
int someMask;
//...
void Awake(){
someMask = LayerMask.GetMask("someMask");
}
Using Masks the raycast will ignore any object that is not marked with this mask. To set the mask of an Object click on it, on the inspector there is a drop down named Layer on the top click on it go to AddLayer add your Layer (just a name) and then go back to your object and set the Layer, as soon you finish this add the above code and make sure in the "GetMask("someMask")" the someMask matches the name of the Layer you created.You should use this instead of my initial suggestion. Also another way would be to add the IgnoreRaycast layer(mask) on all objects you want to be ignored but i don't like it.
hey man think you could help out with my question! That is if you know multiplayer
haha was talking about the other guy xD thanks anyway tho
Thanks for the information explaining how to do masks. It is what I have been looking for.
Answer by ITZZZETHAN · May 03, 2017 at 12:37 PM
have you put this in void Update () if not I suggest you do that might fix your problem! also this
if(hit.collider != null) {
if (hit.transform.name == "PlayBt") {
Debug.Log ("yeah");
do this instead:
if (hit.collider != null) if (hit.transform.name == "PlayBt")
{
Debug.Log("Yeah");
}
I hope this fixes your issue!
Your answer
Follow this Question
Related Questions
Hide/reveal shader and material to make scene appear on touch event 0 Answers
How to allow clicks to go through Circle Collider 2D on sprite prefab? 0 Answers
How to show my Buttons over texture ? 0 Answers
2D Top Down, Enemy facing player slow rotation issue. Vector gets messed up after collision. 3 Answers
Sprites showing through each other in 3D 0 Answers