Home > Net >  Draw line between two points at runtime using Line Renderer
Draw line between two points at runtime using Line Renderer

Time:02-23

I'm new to coding and trying to make a game where objects appear on a wall, and players will connect 2 that match via a line (this game will feel 2D but is in fact 3D).

My first step is just to be able to draw a line between 2 points. Later I'll try to figure out whether to use bools, or triggers, or colliders or what to determine whether the player connected the correct objects.

I'm having a lot of trouble with this. I want to click the screen once to determine a starting point, then a second time to determine an end point for the line renderer in unity. Then have this all repeat with a new line.

This script is almost working, but for some reason my first point is always set to (0, 0, 0), and I have no idea why. This is my current script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawLine_2PT : MonoBehaviour


{
    // Apply these values in the editor
    public LineRenderer Line;
    public GameObject newLine;
   
    public float minimumVertexDistance = 0.1f;

    public Vector3 GetWorldCoordinate(Vector3 mousePosition)
    {
        Vector3 mousePos = new Vector3(mousePosition.x, mousePosition.y, 1);
        return Camera.main.ScreenToWorldPoint(mousePos);
    }

    void Start()
    {
        // set the color of the line
        Line.startColor = Color.red;
        Line.endColor = Color.red;

        // set width of the renderer
        Line.startWidth = 0.3f;
        Line.endWidth = 0.3f;
        Line.positionCount = 0;
    }


    void Update()
    {
        if ((Input.GetMouseButtonDown(0)) && Line.positionCount == 0)

        {
            Vector3 mousePos = GetWorldCoordinate(Input.mousePosition);
            Line.SetPosition(0, mousePos);
            Line.positionCount = Line.positionCount   2;
            
               
        }

        
        if (Input.GetMouseButtonDown(0) && Line.positionCount == 2)

        {
            Vector3 mousePos = GetWorldCoordinate(Input.mousePosition);
            Line.SetPosition(1, mousePos);
            Instantiate(newLine, transform.position, Quaternion.Euler(0, 0, 0));
            GetComponent<DrawLine_2PT>().enabled = false;

        }

    }

}

The last bit spawns a new line after the 2nd point is made, and turns off the initial line renderer. Again, seems to be working except for the first point always spawning at (0, 0, 0) instead of the mouse position.

Any/all advice is much appreciated.

Thanks

CodePudding user response:

You are doing

Line.positionCount = Line.positionCount   2;

after you call

Line.SetPosition(0, mousePos);

so this has no effect yet since there are no points yet when you try to set it.

And then both positions you add just keep the default position 0,0,0 until you override the second one later.

You should first increase the count and then set the position.

Also note that actually both your code blocks will be executed in the same frame since after increasing the count the second condition matches as well. Is this intended?

I would rather expect something like e.g.

void Update()
{
    if ((Input.GetMouseButtonDown(0))
    {
        if(Line.positionCount == 0))
        {
            Line.positionCount = 1;
            var mousePos = GetWorldCoordinate(Input.mousePosition);
            Line.SetPosition(0, mousePos);            
        }    
        else
        {
            Line.positionCount = 2;
            var mousePos = GetWorldCoordinate(Input.mousePosition);
            Line.SetPosition(1, mousePos);
            Instantiate(newLine, transform.position, Quaternion.Euler(0, 0, 0));
            GetComponent<DrawLine_2PT>().enabled = false;
        }
    }
}

And if you want to you could even visually update the line while the user is drawing it like e.g.

void Update()
{
    if ((Input.GetMouseButtonDown(0))
    {
        if(Line.positionCount == 0))
        {
            Line.positionCount = 1;
            Vector3 mousePos = GetWorldCoordinate(Input.mousePosition);
            Line.SetPosition(0, mousePos);            
        }    
        else
        {
            Line.positionCount = 2;
            var mousePos = GetWorldCoordinate(Input.mousePosition);
            Line.SetPosition(1, mousePos);
            Instantiate(newLine, transform.position, Quaternion.Euler(0, 0, 0));
            GetComponent<DrawLine_2PT>().enabled = false;
        }
    }
    else if(Line.positionCount == 1)
    {
        var mousePos = GetWorldCoordinate(Input.mousePosition);
        Line.SetPosition(1, mousePos);
    }
}
  • Related