- Home /
Question by
Razputin · Apr 13, 2016 at 10:24 AM ·
raycastmousepositionhitraycasthit2d
Hit is always returning null for some reason.
Why does my hit always return null? Is it because I'm not using Hit Out? Hit out didn't appear to even work in this context.
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2(FirePoint.position.x, FirePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast(firePointPosition, (mousePosition - firePointPosition) * 100);
Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100);
if (hit.collider != null)
{
Debug.Log("Hit wasn't null");
}
if(hit.collider == null)
{
Debug.Log("We messed up");
}
Comment
Best Answer
Answer by aditya · Apr 13, 2016 at 10:32 AM
may be your gameObject that your are trying to detect the rayCastHit is missing its collider, coz Raycast only works on colliders, PLUS
i would like you to reCheck your direction calculation if it is giving you the correct direction for raycast
PLUS
: You should normalize your direction (mousePosition - firePointPosition).normalized * 100f
Your answer
Follow this Question
Related Questions
Raycast Hit Question 1 Answer
2D - finding object at mouse position 2 Answers
How do I move only one selected object, rather than all objects with that script? 3 Answers
Help with LineRenderer 1 Answer