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}");}
> }
> }