- Home /
Why doesn't the distance in RaycastAll and DrawRay correspond?
Hi! My problem is that the length/distance of the ray drawn is not the same as the ray being cast, even though the numbers are the same. The ray is only as long as about 2/3 of the ray that is shown. I'm aware that the length of the ray is made differently in DrawRay, but to my knowledge it doesn't matter as long as you use the same value (for me - 7). This problem makes it difficult to test my game. I'm making a 2D sidescroller.
Here's the code. I've only included the relevant part of the code. I don't know, but it might be relevant to mention that the script is attached to a player object with 2 colliders on it.
var hit : RaycastHit2D[];
function FixedUpdate()
{
hit = Physics2D.RaycastAll(transform.position - Vector2(-0.3, 0.2), Vector2(1, 1), 7);
Debug.DrawRay(transform.position - Vector3(-0.3, 0.2, 0), Vector2(1, 1) * 7, Color.white);
}
I've tried to replace the dir
parameter in DrawRay
with a Vector3 but nothing changes. I've also tried checking if the ray is being cast to the side along the z-axis in the scene view, which it doesn't.
Thanks!!
Answer by ThorShaker · Mar 19, 2014 at 04:45 PM
I solved it.
Debug.DrawRay(transform.position - Vector2(-0.3, 0.2), Vector2(1, 1).normalized * 7, Color.white);
I put in a Vector2 for consistency, as it didn't make a practical difference. The thing that fixes it is ".normalized". For some reason DrawRay doesn't normalize automatically as in RaycastAll, so you have to do it manually. Vector2.normalized from the Script Reference:
Returns this vector with a magnitude of 1 (Read Only). When normalized, a vector keeps the same direction but its length is 1.0.
So when the vector isn't normalized, its length is something other than 1. That explains why I got another length than 7 units.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140637im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I draw a specific distance with Debug.Drawray 4 Answers
How to control multiple gradients 0 Answers
Raycasting in 2D is not working 0 Answers
OnCollisionEnter2D is not working 0 Answers
How to place an object in the direction of a raycast 1 Answer