- Home /
Question by
fadisharif15 · Apr 23, 2018 at 06:03 PM ·
collision2d gamestars
create game 2d linking stars
Hello every body
I am a novice game developer I'm working on a two-dimensional game project He needed to develop the game of connecting the stars to the mobile environment. The way is to click on the first star and drag (with a line) until you reach the second star and then the line you have connected appears. I have now completed the drawing stage using Line Renderer and need help in detecting the collision between the line and the star.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawLine : MonoBehaviour
{
private LineRenderer line;
private bool isMousePressed;
public List<Vector2> pointsList;
private Vector2 mousePos;
public EdgeCollider2D egdeCol;
// Structure for line points
void Awake()
{
// Create line renderer component and set its property
line = gameObject.AddComponent<LineRenderer>();
line.material = new Material(Shader.Find("Sprites/Diffuse"));
line.SetVertexCount(0);
line.sortingOrder = 1;
line.SetWidth(1f, 1f);
line.numCapVertices = 90;
line.SetColors(Color.green, Color.green);
line.useWorldSpace = true;
isMousePressed = false;
pointsList = new List<Vector2>();
}
void Update()
{
// If mouse button down, remove old line and set its color to green
if (Input.GetMouseButtonDown(0))
{
isMousePressed = true;
line.SetColors(Color.green, Color.green);
}
if (Input.GetMouseButtonUp(0))
{
isMousePressed = false;
//line.SetVertexCount(0);
//pointsList.RemoveRange(0, pointsList.Count);
}
// Drawing line when mouse is moving(presses)
if (isMousePressed)
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (!pointsList.Contains(mousePos))
{
pointsList.Add(mousePos);
line.SetVertexCount(pointsList.Count);
line.SetPosition(pointsList.Count - 1, (Vector2)pointsList[pointsList.Count - 1]);
if (pointsList.Count > 1)
{
egdeCol.points = pointsList.ToArray();
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Why is my character not colliding with other objects. 3 Answers
Generate Tiles Programatically Over Player Position? 0 Answers
Audio Source Play On Collision 2D? 3 Answers
2D, collision with background 0 Answers