Raycast 2d is not working at all
I'm new to Unity and blocked 2nd day with a simple try to raycast. This is the script which I use to raycast:
void Update () {
Debug.DrawLine(transform.position, transform.position + transform.right);
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.position + transform.right, Mathf.Infinity);
if(hit.collider != null)
{
Debug.Log("hit");
}
}
I have attached this script to a square and put near it another square which acts like a target, also added a 2d box collider to the target. I have disabled the "hit itself" feature like is described here: http://answers.unity3d.com/questions/756380/raycast-ignore-itself.html After performing all this steps the raycast hits nothing, the collider of the hit object is always null (checked this in debug mode, also nothing is written in console) . I drew a debug line, and indeed it points to the target square like in screenshots.
Please help me figure out what I'm doing wrong.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134328im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Linecast ignores obstacles. 0 Answers
Is there any way how to do raycasts like this? 0 Answers
I can't get RayCast to report the collider I want. 1 Answer
Detect when Raycast DOESN'T hit layer. 1 Answer
RayCast2D and RayDraw errors 0 Answers