I can't store raycast hit data in a variable?!
Now I've been getting into coding and I'm working on my first game. In my player movement script I'm using raycasts on the opposite ends of the player collider to check if the player is grounded. The script worked fine in the beginning, but when I try to store hit information in a variable, the entire raycast recieves an error. Now I have no idea what is causing this but it's pretty inconvenient since the player is grounded if the raycasts collide with anything, excluding ground. If anyone knows a solution that would help a lot since I'm probably going to use raycasts a lot.
Thanks in advance!
private bool GroundRaycast()
{
Vector2 LeftRayStart;
Vector2 RightRayStart;
LeftRayStart = box.bounds.center;
RightRayStart = box.bounds.center;
LeftRayStart.x -= box.bounds.extents.x;
RightRayStart.x += box.bounds.extents.x;
float RayLength = (box.bounds.size.y / 2) + 0.2f;
Debug.DrawRay(LeftRayStart, Vector2.down, Color.green);
Debug.DrawRay(RightRayStart, Vector2.down, Color.red);
RaycastHit2D lefthit;
if (Physics2D.Raycast(LeftRayStart, Vector2.down, out lefthit, RayLength))
{
return true;
}
if (Physics2D.Raycast(RightRayStart, Vector2.down, RayLength))
{
return true;
}
return false;
}
Your answer
Follow this Question
Related Questions
AI Raycasting 0 Answers
C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer
Boolean variable won't trigger in other script 1 Answer
Can't Add Script Behavior... 2 Answers
Script error and no Variables showing 0 Answers