Skip to content

Instantly share code, notes, and snippets.

Dave Pape davepape

Block or report user

Report or block davepape

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@davepape
davepape / mouseTriangles.cs
Last active Oct 11, 2018
Make a trail of triangles that follow the mouse
View mouseTriangles.cs
// 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;
@davepape
davepape / triangles2.cs
Created Oct 11, 2018
Create a Unity Mesh with 2 triangles
View triangles2.cs
// Creating Mesh data in Unity
// This script should be attached to an empty GameObject. It requires vertexColor.shader (https://gist.github.com/davepape/0dee3b1550a1f4159bc173e1b32dbe0c)
// On startup, it will create a MeshFilter containing 2 triangles, and apply a Material.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class triangles2 : MonoBehaviour {
void Start ()
@davepape
davepape / vertexColor.shader
Created Oct 11, 2018
minimal Unity shader, to use vertex colors without lighting or texture
View vertexColor.shader
// taken from http://answers.unity3d.com/questions/391561/create-a-mesh-and-color-cubes.html
Shader "Custom/Vertex Colored"
{
Properties
{
}
SubShader
{
Pass
{
@davepape
davepape / triangles.cs
Last active Oct 11, 2018
Create a Unity mesh of 2 triangles, in C#
View triangles.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class triangles : MonoBehaviour {
void Start ()
{
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
View statemachine.cs
// Example of a simple state machine.
// The script has 3 states for different actions it might be performing. It transitions between the states based on user input.
// State "start" does nothing; state "slowcube" makes the object named "cube" slow down; state "spiralsphere" makes the sphere object spiral outward.
// It can transition from "start" to "slowcube" OR "spiralsphere" depending on what button is hit.
// After that, it can transition from "slowcube" to "spiralsphere" or from "spiralsphere" to "slowcube" when fire1 is hit.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class statemachine : MonoBehaviour {
View mcp.cs
// Unity3d example of using Find() and GetComponent() to control one script from another script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mcp : MonoBehaviour {
void Start () {
}
View createCirclingChildren.cs
// Unity3d script to animate many circling objects
// Script creates the objects on startup, by instantiating a given object (create a prefab and assign it to the "obj" parameter in the inspector)
// Then moves them in a circle in the same way as circleChildren.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class createCirclingChildren : MonoBehaviour {
public int numberOfChildren = 1;
View circleChildren.cs
// Unity3D script to move all the children of an object in a circle
// Uses "childCount" and "GetChild()" to access all the immediate child nodes
// Positions each on a circle, spreading them evenly around the circle
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class circleChildren : MonoBehaviour {
public float speed = 1f;
View circle2.cs
// Move an object in a circle in Unity3d, by "driving"
// Assumes that the object is currently on the edge of a circle, and computes an angle tangent to that circle
// Sets the objects orientation to that angle, and then moves the object "forward" (the "up" vector in this case, since we're working in XY)
// Note that moving forward will actually cause the object to move slightly off of its current circle, and thus spiral away from the origin (this becomes more obvious at higher speeds)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class circle2 : MonoBehaviour {
View circle1.cs
// Minimal Unity3d example of moving an object in a circle
// Script determines a current angle, then computes an X & Y for that angle using cos & sin
// Object is placed at the computed X,Y by setting the "localPosition" attribute of its transform
// Script uses 2 public variables so that you can control the size of the circle and how fast it moves
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class circle1 : MonoBehaviour {
You can’t perform that action at this time.