This post has been wikified, any user with enough reputation can edit it.
Question by
DDAAACCC · Jul 22, 2020 at 01:06 PM ·
linerendererline drawing
If there is only one area specified by this line
I am drawing a line to detect objects in a specific area
However, as I draw the line, I know that if you do it wrong once, you can create two areas.
Vector3 startPosition;
Vector2 preivousPos=new Vector2(100,100);
GameObject currentLineObject;
LineRenderer currentLineRenderer;
public Material lineMaterial;
public float lineThickness;
int vertexnum = 0;
CameraManager cameraManager;
private void Start()
{
cameraManager = Camera.main.GetComponent<CameraManager>();
}
void LateUpdate()
{
//Draw line
if (cameraManager.mouseClickMode==3&& Input.GetMouseButtonDown(0)) //한번 누르면 계속눌러도 한번만
{
StartDrawingLine();
}
else if (cameraManager.mouseClickMode == 3 && Input.GetMouseButton(0)) // 누르는 동안 계속
{
Vector2 vec2 = new Vector2(GetMousePosition().x, GetMousePosition().y);
if (Vector2.Distance(vec2, preivousPos)>0.05)
{
currentLineRenderer.positionCount++;
currentLineRenderer.SetPosition(vertexnum++, GetMousePosition());
}
}
else if (cameraManager.mouseClickMode == 3 && Input.GetMouseButtonUp(0)) //누르고 난후에
{
EndDrawingLine();
}
//Draw line
if (cameraManager.mouseClickMode == 4 && Input.GetMouseButtonDown(0)) //한번 누르면 계속눌러도 한번만
{
StartDrawingLine();
}
else if (cameraManager.mouseClickMode == 4 && Input.GetMouseButton(0)) // 누르는 동안 계속
{
Vector2 vec2 = new Vector2(GetMousePosition().x, GetMousePosition().y);
if (Vector2.Distance(vec2, preivousPos) > 0.1)
{
currentLineRenderer.positionCount++;
currentLineRenderer.SetPosition(vertexnum++, GetMousePosition());
}
}
else if (cameraManager.mouseClickMode == 4 && Input.GetMouseButtonUp(0)) //누르고 난후에
{
if(Vector2.Distance(startPosition,
new Vector2(GetMousePosition().x, GetMousePosition().y))<0.2)
{
currentLineRenderer.positionCount++;
currentLineRenderer.SetPosition(vertexnum++, startPosition);
startPosition = Vector3.zero;
currentLineObject = null;
currentLineRenderer = null;
vertexnum = 0;
preivousPos = Vector2.zero;
}
else
{
Destroy(currentLineObject);
startPosition = Vector3.zero;
currentLineObject = null;
currentLineRenderer = null;
vertexnum = 0;
preivousPos = Vector2.zero;
}
}
}
Vector3 GetMousePosition()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
Input.mousePosition.y, -Camera.main.transform.position.z ));
return mousePos;
}
void StartDrawingLine()
{
startPosition = GetMousePosition();
currentLineObject = new GameObject();
currentLineObject.transform.position = startPosition;
currentLineObject.transform.SetParent(transform);
currentLineRenderer = currentLineObject.AddComponent<LineRenderer>();
currentLineRenderer.positionCount = 0;
currentLineRenderer.material = lineMaterial;
currentLineRenderer.startWidth = lineThickness;
currentLineRenderer.endWidth = lineThickness;
}
void EndDrawingLine()
{
startPosition = Vector3.zero;
currentLineObject = null;
currentLineRenderer = null;
vertexnum = 0;
preivousPos = Vector2.zero;
}
public void destoryLine()
{
for(int i=0; i<transform.childCount;i++)
{
GameObject.Destroy(transform.GetChild(i).gameObject);
}
}
How can I detect objects in a painted area?
Prevent two areas from appearing like photoshop lassotool
111111.png
(29.0 kB)
linebake.png
(27.2 kB)
Comment