- Home /
Question by
megamika2 · Apr 05, 2018 at 07:59 PM ·
2draycastraycasthitraycasthit2d
How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider.
I have created a ray and i need to get an information of how far the ray acually gone before meeting a collider2d.
Here is what i want to achieve.
if (Physics2D.Raycast(transform.position + transform.TransformDirection(RightRayOffset_Pos), transform.TransformDirection(RightRay), 0.22f, Environment))
{
Debug.Log(/* piece of code that gets the distance from object to hit */);
}
Comment
Best Answer
Answer by megamika2 · Apr 06, 2018 at 07:48 AM
Basically the problem was solved by assighning all of the raycst script to a RaycastHit2d and then unsing RaycastHit2d.Distance to get the distance. Here is what what I mean.
HitR = Physics2D.Raycast(transform.position + transform.TransformDirection(RightRayOffset_Pos), transform.TransformDirection(RightRay), 0.22f, Environment);
if (HitR)
{
Debug.Log(HitR.distance);
}
Answer by bhavinbhai2707 · Apr 05, 2018 at 08:51 PM
you can use Vector2.Distance(transform.position,hit.gameobject.transform.position);
To know more click here
Yes your answer is correct but I dont have RaycastHit2d hit and so I have added it. And done in other way.