-1

I'm in the middle of a game jam and I've been looking for 3 hours why my RaycastALL doesn't detect every collision. Here's the bit of code that handles this. Thank you very much for your time. I'm available to answer all your questions regarding my project :

            case (PlayerState.CarryingRaw):
                //Debug.Log("Carrying" + pickedUpRawItems.Count + "items");
                //Debug.Log(pos.x +","+ pos.y);
                hits = Physics2D.RaycastAll(transform.position, new Vector2(pos.x-5,pos.y),20,8);
                Debug.DrawLine(transform.position, new Vector2(pos.x - 5, pos.y), Color.red, 2);

                for (int i = 0; i < hits.Length; i++)
                    {
                        Debug.Log(hits[i].transform.name);
                    }

                //Debug.Log("Raycasted" + hits.Length + "Objects");
                for (int i = 0; i < hits.Length; i++)
                {
                    {
                        if (hits[i].transform.tag == pickedUpRawItems[pickedUpRawItems.Count - 1].transform.tag)
                        {
                            Debug.Log(hits[i].transform.tag);
                            Debug.Log(pickedUpRawItems[pickedUpRawItems.Count - 1].transform.tag);
                            hits[i].transform.GetComponent<BoxCollider2D>().enabled = false;
                            hits[i].transform.GetComponent<ItemScript>().currentState = ItemScript.ObjectState.PickedUp;
                            hits[i].transform.localScale = sizeChange;
                            pickedUpRawItems.Add(hit.transform.gameObject);
                        }
                    }
                }
                break;
            case (PlayerState.Working):
                break;
            case (PlayerState.Free):
                Debug.Log("youhou + State.Free");
                hit = Physics2D.Raycast(transform.position, new Vector2(pos.x - 5, pos.y));

                if (hit)
                {
                    hit.transform.GetComponent<BoxCollider2D>().enabled = false;
                    Debug.Log("youhou + hit.Free");
                    hit.transform.GetComponent<ItemScript>().currentState = ItemScript.ObjectState.PickedUp;
                    hit.transform.localScale = sizeChange;
                    pickedUpRawItems.Add(hit.transform.gameObject);
                    currentState = PlayerState.CarryingRaw;
                }
                break;

1 Answers1

0

Not enough info to be able to answer this but I would start troubleshooting with adding Debug.Log() calls all over the place. Log out the collider name for what is being hit, the case that is calling it, use your debug line in all cases.

I'd also recommend adding a layer mask for performance reasons so as not to try and raycast every single layer.

Also, check your physics collision settings. If you go to Project Settings > Physics2d is your raycasting object set to collide with all of the objects you're trying to raycast against?

Do all items you're trying to raycast against have colliders?

Is your raycast distance long enough?

Here is a similar question.

https://forum.unity.com/threads/physics2d-raycastall-not-detecting-colliders-minor-problem.862744/

tenn1440
  • 41
  • 5
  • I can't thank you enough for your advice. My boxcollider was up but was very small compared to the actual sprite. It was as simple as that. Thank you, again. – Aziz Goumiri Jul 10 '21 at 19:04
  • If this fixed your problem you should accept the answer (the little check mark below the vote arrows). – David Jul 10 '21 at 20:12
  • accepting an answer lets others know of possible working solutions and also increases the rep of the person answering. – David Jul 10 '21 at 20:13