Skip to content

Instantly share code, notes, and snippets.

@HectorPulido
Last active October 12, 2017 19:14
Show Gist options
  • Save HectorPulido/47c4f2b7f145eca682d3d83c75bf8d85 to your computer and use it in GitHub Desktop.
Save HectorPulido/47c4f2b7f145eca682d3d83c75bf8d85 to your computer and use it in GitHub Desktop.
This script allows you to make physics drawings on unity using line render
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
Hola amigo, este paquete ha sido creado por Hector Pulido de youtube
YOUTUBE:https://www.youtube.com/channel/UCS_iMeH0P0nsIDPvBaJckOw
Apoyame en PATREON: https://www.patreon.com/HectorPulido
Sígueme en TWITTER: https://twitter.com/Hector_Pulido_
Acompañanos en FACEBOOK: https://www.facebook.com/groups/Unity3DenEspanol
Unetenos en GOOGLE+: https://plus.google.com/communities/103453576782892092582
*/
public class Draw: MonoBehaviour
{
public bool usePhysics = true;
public float colliderThickness = 0.1f;
public float vertexPrecision = 0.01f;
public Shader shader;
public Color color;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(draw());
}
}
Vector3 newVertex;
Vector3 lastVertex;
IEnumerator draw()
{
LineRenderer r = new GameObject().AddComponent<LineRenderer>();
r.transform.SetParent(transform);
r.startWidth = 0.1f;
r.endWidth = 0.1f;
r.material = new Material(shader);
r.material.color = color;
List<Vector3> posiciones = new List<Vector3>();
while (Input.GetMouseButton(0))
{
newVertex = Camera.main.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward * 5;
if (Vector3.Distance(lastVertex, newVertex) >= vertexPrecision) //Esto mejora la calidad de la linea
{
posiciones.Add(newVertex);
r.positionCount = posiciones.Count;
r.SetPositions(posiciones.ToArray());
lastVertex = newVertex;
}
yield return new WaitForEndOfFrame();
}
r.useWorldSpace = false;
if (usePhysics)
{
List<Vector2> posiciones2 = new List<Vector2>();
for (int i = 0; i < posiciones.Count; i++)
{
posiciones2.Add(new Vector2(posiciones[i].x, posiciones[i].y));
}
for (int i = posiciones.Count - 1 ; i > 0 ; i--) // Esta parte permite colliders concavos
{
posiciones2.Add(new Vector2(posiciones[i].x, posiciones[i].y + colliderThickness));
}
PolygonCollider2D col = r.gameObject.AddComponent<PolygonCollider2D>();
col.points = posiciones2.ToArray();
col.gameObject.AddComponent<Rigidbody2D>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment