Raycast transform.forward does not rotate up

Hi all! Total newbie here.

I have a code that checks whether there is a direct line of sight between enemy and player.
I’m raycasting in front of object “Enemy” using transform.forward, but I have trouble detecting player when he reaches some height.

Everything works fine when rotating y axis (ie. object is turning left and right), however it does not seem to be rotating upwards. I tried rotating the transform.forward with Quaternion.Euler(transform.rotation.x, etc, etc), but it changed nothing.

The transform.forward itself works, as I use Gizmos.DrawLine, but the raycast detection does not work.


(these situations are extreme but I can’t make a screenshot of enemy approaching while being turned only slightly - it should stop at that point, because it attacks when the raycast hits the player)

You can see the magenta lines go through the player, whether I’m above the enemy, or standing right on top of the enemy. However the raycast just does not hit. I use transform.LookAt() to rotate the enemy.

Code snippet with raycast:

void Update()
{
    //removed unrelated

    RaycastHit lineOfSight0;
    RaycastHit lineOfSight1;
    
    bool sight0 = Physics.Raycast(transform.position + transform.right * 0.1f, transform.forward, out lineOfSight0);
    bool sight1 = Physics.Raycast(transform.position - transform.right * 0.1f, transform.forward, out lineOfSight1);

    if (sight0 && sight1)
        playerInLineOfSight = lineOfSight0.collider.gameObject.layer == 11 && lineOfSight1.collider.gameObject.layer == 11;
    else
        playerInLineOfSight = false;

    //removed unrelated

    transform.LookAt(player);

    }
}
void OnDrawGizmosSelected()
{
    //removed unrelated
    Gizmos.color = Color.magenta;
    Gizmos.DrawLine(transform.position + transform.right * 0.1f, transform.position + transform.right * 0.1f + transform.forward * attackRange);
    Gizmos.DrawLine(transform.position - transform.right * 0.1f, transform.position - transform.right * 0.1f + transform.forward * attackRange);
}

It’s weird that it works no matter how the y axis is rotated, only the other axes don’t work.
The playerInLineOfSight boolean remains false even though it should be true.

I’m really scratching my head over this.
Please help. :))