- Home /
Can RaycastHit2D.point be inside a BoxCollider2D if raycast was performed from outside that collider?
I'm working on a 2D platformer without using physics. Sometimes happens that my character, moving to the right, gets stuck inside a wall. My code is:
//horizontalSpeed is > 0
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, horizontalSpeed, LayerMask.GetMask("Walls"));
if (hit)
{
float margin = 0.01f;
transform.position = new Vector2(hit.point.x + boundingBox.offset.x - boundingBox.bounds.extents.x - margin, transform.position.y);
horizontalSpeed = 0;
}
else
transform.position = new Vector2(transform.position.x + horizontalSpeed, transform.position.y);
Could this problem be caused by hit.point to be inside the Wall's box collider instead of being on the edge?
Answer by MelvMay · Aug 24, 2015 at 10:45 AM
No, it will always be a contact point outside of the collider. The only exception is if the start point of the ray is inside the collider but whether this point is returned or not is controlled via the following property: Physics2D.raycastsStartInColliders
Thank you for your answer! Then probably the problem of my character getting stuck is caused by something else. Good to know! :)
Your answer
Follow this Question
Related Questions
Why is RaycastHit2d Not working 0 Answers
What's wrong with my RayCastHit2D? 0 Answers
Raycasting not behaving as expected? 2 Answers
My Raycast2Dhit, isnt working on prefabs 0 Answers
How to get co-ordinates of end point of Raycast2D ? 1 Answer