Raycast hits Boxcollider2D outside of set bounds
I have my boxcollider2d set like this:
However, the further the object gets away from 0,0,0, the more the collider extends to the left and the raycast detects it:
Here's my raycast code:
RaycastHit2D hit = Physics2D.Raycast((Vector2)camera.ScreenToWorldPoint(Input.mousePosition), (Vector2)camera.ScreenToWorldPoint(Input.mousePosition));
if (hit.collider == null)
{
targetPos = (Vector2)camera.ScreenToWorldPoint(Input.mousePosition);
target = null;
Debug.Log(hit.collider);
}
if (hit.collider)
{
target = hit.collider.transform.GetComponent<UnitHealth>();
targetPos = (Vector2)hit.transform.position;
Debug.Log(hit.collider);
Debug.Log(hit.collider.transform.position);
Debug.Log(hit.collider.bounds);
Thank you in advance!
boxcollider2d.png
(52.3 kB)
boxcollider-moved.jpg
(20.8 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613012928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to ensure raycasts hit colliders inside of trigger colliders on objects that have a rigidbody? 0 Answers
When raycasting, script doesn't detect anything coming into ray yet I did everything right! 1 Answer
How best to detect when a charcter object cannot move? 3 Answers
Collider does not always detect OnTriggerEnter 2 Answers
Particle Collision Bug. 1 Answer