Last active
October 11, 2018 16:23
-
-
Save davepape/4098befcd506c4f00a975ea484731ef2 to your computer and use it in GitHub Desktop.
Make a trail of triangles that follow the mouse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Draw a set of triangles that follow the mouse. | |
// This script should be attached to a GameObject that has a MeshRenderer (with Material). | |
// The script will create a MeshFilter and fill it with triangles. On each frame, one triangle will be changed to appear at the mouse position. It cycles through all the triangles, to create a tail of fixed length. | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class mouseTriangles : MonoBehaviour { | |
public int numTriangles=1; | |
private int numVerts=3, curVert=0; | |
private Vector3[] verts; | |
private Mesh myMesh; | |
void Start () | |
{ | |
gameObject.AddComponent<MeshFilter>(); | |
myMesh = GetComponent<MeshFilter>().mesh; | |
numVerts = numTriangles * 3; | |
verts = new Vector3[numVerts]; | |
for (int i=0; i < numVerts; i++) | |
verts[i] = new Vector3(0f, 0f, 0f); | |
myMesh.vertices = verts; | |
int[] tris = new int[numVerts]; | |
for (int i=0; i < numVerts; i++) | |
tris[i] = i; | |
myMesh.triangles = tris; | |
} | |
void Update () | |
{ | |
verts[curVert] = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x+10, Input.mousePosition.y-5, Camera.main.nearClipPlane)); | |
verts[curVert+1] = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x-10, Input.mousePosition.y-5, Camera.main.nearClipPlane)); | |
verts[curVert+2] = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y+5, Camera.main.nearClipPlane)); | |
curVert = (curVert+3) % numVerts; | |
myMesh.vertices = verts; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment