ShootAction how to draw lines from selected unit to all targets

Hi everyone =)

I am curretly trying to make some additions to the ShootAction script so when you select the action it draws a small line from your selected unit towards all targets that are in range.

i currently have made a simple function that runs through the enemy unit list, and draws a debug line towards the enemies it looks fine in the scene view and all but i would like it to show up doing runtime in the game view so the player can see all the targets that are currently in range.

private void DrawTargetLines() {
        foreach (Unit targetUnit in enemyUnitList) {
            Vector3 playerPosition = unit.GetWorldPosition();
            Vector3 targetPosition = targetUnit.GetWorldPosition();
            
            Debug.DrawLine(playerPosition + Vector3.up, targetPosition + Vector3.up, Color.white);
        }
    }

i have tried using the line renderer however it simply connected the enemies with one line.

does anyone have a good idea of what i could try to use or how to fix this?

Bear in mind that the Debug.DrawLine won’t work in a built game. :slight_smile:

For the LineRenderer, you’ll need to create a unique Line Renderer for each of the targets. Make a prefab with a LineRenderer in it.
Then add this to your ShootAction:

    [SerializeField] private LineRenderer lineRendererPrefab;
    void DrawLines()
    {
        foreach (GridPosition gridPosition in GetValidActionGridPositionList())
        {
            LineRenderer lineRenderer = Instantiate(lineRendererPrefab, transform.position, Quaternion.identity);
            lineRenderer.positionCount = 2;
            lineRenderer.SetPosition(0, transform.position);
            lineRenderer.SetPosition(1, LevelGrid.Instance.GetWorldPosition(gridPosition));
        }
    }

Hi Brian.
Thank you for the reply, I kept laughing of myself why I didn’t think of having it instantiate the line renderer game objects and parent it to the unit ^^ but thank you for the quick reply =D

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms