DrawLine changing depending on rotation

I have a script to draw rays in the shape of a cone and it works fine when the gameobject is facing directly forwards or backwards but as soon as i change the rotation it starts to change shape and flatten into a 2d cone

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
using static UnityEditor.PlayerSettings;

public class Raycasting : MonoBehaviour
{
    public float MaxDistance = 100f;
    public float Radius = 5f;
    public int VertexCount = 10;
    public LayerMask Buildings;

    private void Update()
    {
        float deltaTheta = (2f * Mathf.PI) / VertexCount;
        float theta = 0f;

        Vector3 oldPos = Vector3.zero;
        for (int i = VertexCount; i >= 0; i--)
        {
            Vector3 pos = new Vector3(Radius * Mathf.Cos(theta), Radius * Mathf.Sin(theta), 0f);
            Vector3 EndPos = (MaxDistance * transform.forward) + (transform.position + pos);

            if (Physics.Raycast(transform.position, EndPos - transform.position, MaxDistance, Buildings))
            {
                DrawRay(oldPos, pos, true);
            }
            else
            {
                DrawRay(oldPos, pos, false);
            }
            oldPos = transform.position + pos;

            theta += deltaTheta;
        }
    }

    private void DrawRay(Vector3 OldPos, Vector3 Pos, bool Hit)
    {
        Vector3 EndPos = (MaxDistance * gameObject.transform.TransformDirection(gameObject.transform.forward)) + (transform.position + Pos);
        if (Hit)
        {
            Debug.DrawLine(transform.position, EndPos, Color.green);
        }
        else
        {
            Debug.DrawLine(transform.position, EndPos, Color.red);
        }
    }
}

This usually was happening for me when I somewhere use global Vector3 instead of the local one. That being said I suspect that an error is somewhere here

    Vector3 EndPos = (MaxDistance * transform.forward) + (transform.position + pos);

at this part (transform.position + pos);

This not working well because that pos should be “turned” relative to your shooter position. Try maybe replace “pos” here with

    tranform.TransformDirection(pos)

Sorry if that didn’t solve it, but I hope I gave you a hint at least.