Skip to content

Instantly share code, notes, and snippets.

@davepape davepape/mouseTriangles.cs
Last active Oct 11, 2018

What would you like to do?
Make a trail of triangles that follow the mouse
// 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 ()
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
You can’t perform that action at this time.