- Home /
Raycast2D only detects objects directly over origin
I am making a raycast2D hit detection system for my project however I cannot get my ray to detect anything that isn't directly over the center of my character.
I thought that I may accidentally be casting straight up or down, or perhaps that it has zero length, but I created a Debug Ray using the exact same values in the same code and it gives me a ray that looks accurate.
Raycast 2D Code:
var hit: RaycastHit2D = Physics2D.Raycast(transform.position, transform.up, 1000 );
if (hit.collider != null)
{
Debug.Log(hit.collider.name + " HIT");
}
Debug Raycast Code:
Debug.DrawRay(transform.position, transform.up, Color.green);
Using this current code I have to be directly over the collider in order to detect it and I cannot figure out what I am doing wrong since the ray debug seems to suggest its formatted properly. I have tried to use Vector2's for directions however these values don't return any hits even if I am directly over the collider.
Your answer
Follow this Question
Related Questions
Raycast Not Working 3 Answers
My Raycasts seem to sometimes miss 0 Answers
2d Raycast not work 1 Answer
What is the best way to raycast mutliple rays over a line 1 Answer
How to make a Raycast2D proportional to a CanvasScaler dynamic resizing? 0 Answers