Is it possible to Raycast at a child collider instead of its parent?

I’ve been using Raycast as a basic line of sight test for my enemies and it is going well, however no matter which direction I plug in, the Raycast always aims towards the top of the player sprite instead of the collider transform I specified. Through the forums I’ve found that Unity will always use the parent gameobject’s transform due to it having the Rigidbody Component instead of the child collider. I’ve used a vertical offset to fix this issue but that has its own problems.

Is there anyway to raycast towards the child collider or do I just need to use an offset to manually aim the raycast?

Here is the relevant code being used.

> public GameObject _player; //The Game object being called is the child that holds the collider.
> private bool _hasLineOfSight = false;
> public LayerMask layerMask;
> 
> 
>  private void FixedUpdate(){
>     RaycastHit2D ray = Physics2D.Raycast(transform.position, _player.transform.position - 
>     transform.position, Mathf.Infinity, layerMask);
>     Debug.DrawRay(transform.position, _player.transform.position- transform.position, Color.red);
>     if (ray.collider == null){
>       print("Null");
>     } 
>     if (ray.collider != null) {
>       _hasLineOfSight = ray.collider.CompareTag("Player");
>       if(_hasLineOfSight){
>         Debug.DrawRay(transform.position, _player.transform.position - transform.position, Color.blue);
>         Debug.Log($"Hit {ray.collider.tag}");
>        
>    else { Debug.DrawRay(transform.position, _player.transform.position - transform.position, Color.red);
>         Debug.Log($"Hit {ray.collider.tag}");}
>       }
>     }

My goal is to have the Raycast aim at the collider placed at the feet but regardless of what directions I give it, it always aims at the top of the sprites head.

  public GameObject _player;
  private bool _hasLineOfSight = false;


  private void FixedUpdate(){
    RaycastHit2D ray = Physics2D.Linecast(transform.position, _player.transform.position, layerMask);
    Debug.DrawLine(transform.position, _player.transform.position, Color.red);
    if (ray.collider == null){
      print("Null");
    } 
    if (ray.collider != null) {
      _hasLineOfSight = ray.collider.CompareTag("Player");
      if(_hasLineOfSight){
        Debug.DrawLine (transform.position, _player.transform.position, Color.blue);
        Debug.Log($"Hit {ray.collider.tag}");
      } else{
        Debug.DrawLine (transform.position, _player.transform.position, Color.red);
        Debug.Log($"Hit {ray.collider.tag}");
      }
    }
  }

I’ve tried Linecast, but I’ve still gotten the same result.


This is what I what I would like to do without having to manually adjust it

Linecast doesn’t address your issue, it’s simply that it takes two world-space positions to save you calculating the direction.

Therefore the Transform position you’re casting to is at the top of the visuals and the sprite and collider are presumably offset from this.

Obviously you need to get the position of the collider and by far the easiest way to do this it to place the collider at the feet of the player into its own child GameObject and leave the collider offset at (0,0). You can then adjust the Transform instead of the collider offset to move it to the player feet and refer to that Transform instead.

1 Like

That’s it! Thank you so much!

1 Like