DrawRay Hits but RayCast does not
Hello,
I have this script which should create a raycast and print the exact line of the raycast.
Debug.DrawRay(this.transform.position, new Vector3 (1, 1, 0)*7,Color.red,3.0f);
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, new Vector3 (1, 1, 0), 7, hookUpMask);
if (hit.collider != null) {
float distance = Mathf.Abs(hit.point.y - transform.position.y);
Debug.Log (distance);
}
Debug.Break ();
Even though, I have some cases which I can see the line hits the box, but the hit.collider is null... I attach a couple of images (2D view) which did not trigger the hit. Obviously it works if the line goes deeper on the box, but not in this case. Could you help me?
Thanks.
Comment
Answer by Tairos · May 10, 2016 at 08:02 PM
Found the problem, i was assuming vector ( 7,7,0) magnitude is 7, but it's 9.8.... So the DrawRay was longer than my Raycast.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080535im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D TDS Raycast: Drawray vs Line Renderer. 0 Answers
RayCast2D and RayDraw errors 0 Answers
Check if position is inside a collider 5 Answers
onMouseUp 2D Issues 0 Answers