Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Physics2D.RaycastAll not detecting colliders (minor problem...?)

Discussion in '2D' started by hosterweis, Apr 7, 2020.

  1. hosterweis

    hosterweis

    Joined:
    Mar 24, 2020
    Posts:
    4
    Hello, beginner here.

    I created a simple 2D raycastall using a tutorial on Youtube, and the raycast is showing up properly, however my if statement, which should return a debug log, is not a returning anything, which leads me to believe that the raycast is not hitting the collider2d. I have 4 images, all in a row, with collider2d's on each of them. There is an empty game object to the left of the images which holds the code and origin of the ray.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Raycast : MonoBehaviour
    6. {
    7.     string xpos;
    8.     string ypos;
    9.  
    10.     public float maxRayDistance = 15f;
    11.  
    12.     void Update()
    13.     {
    14.         Ray ray = new Ray(transform.position, Vector2.right);
    15.         RaycastHit[] hits = Physics.RaycastAll(transform.position, Vector2.right);
    16.  
    17.         Debug.DrawLine(transform.position, Vector2.right * maxRayDistance, Color.red);
    18.  
    19.         foreach (RaycastHit hit in hits)
    20.         {
    21.             Debug.Log("Hit " + hit.collider.name);
    22.         }
    23.     }
    24.  
    25. }
    26.  
    Any help would be greatly appreciated!
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Are these images Image components? Or are you talking about a GameObject with SpriteRenderer? If Images, these are for the Canvas (UI) and that may be the issue.

    Another source of the issue is likely that you've got a Physics.RaycastAll call instead of a Physics2D.RaycastAll call.
     
    MelvMay likes this.
  3. hosterweis

    hosterweis

    Joined:
    Mar 24, 2020
    Posts:
    4
    Thanks for your reply. These are GameObjects with SpriteRenderers, I believe. Also I'm unable to change the Raycast to Physics2D without an error, so I figured Physics.RaycastAll would work, just without a z-axis.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Physics queries detect physics colliders. They don't detect SpriteRenderers or anything else. You have to choose whether you're using 2D or 3D physics, each have their own components and API and you must use them accordingly. You already stated you were using "collider2d" so you need to use 2D physics API.

    You should post your error. There's docs you can use to figure out what the arguments are and what is returned. They are not identical. 3D has RaycastHit, 2D has RaycastHit2D etc. Use the docs as a resource just like YouTube.

    2D physics and 3D physics are separate systems. There's more difference than the fact that one doesn't use the Z axis. Some things are identical in behaviour, some are similar and some are completely different or don't exist in the other physics system. Use the docs to clarify syntax, arguments etc.


    I would suggest sticking with 2D physics API if you're already using 2D colliders. Check the docs for the 2D raycast query you're using and note the different types, arguments etc.
     
    Last edited: Apr 9, 2020
    hosterweis likes this.