Question by
MomkeyDev · Nov 03, 2020 at 04:03 PM ·
nullreferenceexceptionraycastingraycasthit2d
Raycast2D hit.point is zero ?
So what im trying to do is making my character climb a ledge, but when trying to make a logic where i put my player snap to near the ledge/wall. the hit.point keep returning zero. i do found that the hit.collider return null, thought it did hit something and i don't know why hit.point is zero. any answer will be really appreciated
//what im trying to do in this script is basically snapping my player near the ledge
//when they want to climb, but the hit.point only return zero vector
float LedgeRayDistance = 1f; //The Distance for ray
bool LGU; //BOOL TO CHECK THE TOP OF PLAYER
bool LGM; //BOOL TO CHECK THE MIDDLE PART
bool LGD; //BOOL TO CHECK THE BOTTOM PART
Vector2 LRay2; //The offset for the bottom ray
Vector2 LRay3; //The offset for the middle ray (Sorry for weird numbering)
LayerMask WhatIsLedge; //The layer mask to seperate the player at first
//what this if basically do is to check if both the LGM and LGD is true and LGU is false
//which basically mean they can climb
if (!LedgeCheckUp && LedgeCheckDown | LedgeCheckMiddle)
{
//Change the transform to near the ledge
if (LedgeCheckDown) //If it's on the bottom part that got true
{
//this next logic basically calculate the distance between the hit.point and
//the player position + player collider size in the x (so it doesn't overlap)
Vector2 Ledge2 = (Vector2)transform.position + LRay2; //calculate the
RaycastHit2D hit = Physics2D.Raycast(Ledge2, Vector2.right * transform.localScale.x, LedgeRayDistance);
//Where the problem start, i don't know why but the hit.point is returning zero
Vector2 Ledgepos = hit.point - (Vector2.right * GetComponent<BoxCollider2D>().size.x * transform.localScale.x);
transform.position = Ledgepos;
}
else if (LedgeCheckMiddle) //if the middle part that got true
{
//The same as above. not really important
Vector2 Ledge3 = (Vector2)transform.position + LedgeRay3;
RaycastHit2D hit = Physics2D.Raycast(Ledge3, Vector2.right * transform.localScale.x);
Vector2 Ledgepos = hit.point - (Vector2.right * GetComponent<BoxCollider2D>().size.x * transform.localScale.x);
transform.position = Ledgepos;
}
}
Comment
Your answer
Follow this Question
Related Questions
Select from multiple character to move 0 Answers
NullReferenceException on Raycast 0 Answers
Why this if logic isnot short circuiting? 0 Answers
Rigidbody blocking raycasts 2D 0 Answers