Created
April 14, 2018 10:02
-
-
Save ditzel/f03a37b36ef5fe67c88c0f7ed3fd77ee to your computer and use it in GitHub Desktop.
Mesh from Code
This file contains 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
using UnityEngine; | |
public class HappyCube : MonoBehaviour { | |
MeshFilter mFilter; | |
Mesh Mesh; | |
public float UpDownFactor = 0.1f; | |
public float UpDownSpeed = 6f; | |
public float LeftFactor = 0.3f; | |
public float LeftSpeed = 3f; | |
public float LeftOffset = 2.3f; | |
public float StrechFactor = -0.1f; | |
public float StrechSpeed = 6f; | |
// Use this for initialization | |
void Start () { | |
Mesh = new Mesh(); | |
Mesh.name = "Dancing Cube"; | |
var trigangles = new int[] | |
{ | |
//top/bottom | |
0,2,1, | |
0,3,2, | |
4,5,6, | |
4,6,7, | |
//left/right | |
9,8,11, | |
8,10,11, | |
12,13,15, | |
14,12,15, | |
//front/back | |
16,17,19, | |
18,16,19, | |
20,21,23, | |
22,20,23 | |
}; | |
Mesh.vertices = GenerateVerticies(); | |
Mesh.triangles = trigangles; | |
Mesh.RecalculateBounds(); | |
Mesh.RecalculateNormals(); | |
var mFilter = gameObject.AddComponent<MeshFilter>(); | |
mFilter.mesh = Mesh; | |
} | |
protected Vector3[] GenerateVerticies(float up = 0f, float left = 0f, float strech = 0f, float forward = 0f) | |
{ | |
return new Vector3[] | |
{ | |
//top | |
new Vector3(-1,0,1), | |
new Vector3(1,0,1), | |
new Vector3(1,0,-1), | |
new Vector3(-1,0,-1), | |
//bottom | |
new Vector3(-1 - strech + left,2 + up,1 + strech + forward), | |
new Vector3(1 + strech + left,2 + up,1 + strech + forward), | |
new Vector3(1 + strech + left,2 + up,-1 - strech + forward), | |
new Vector3(-1 - strech + left,2 + up,-1 - strech + forward), | |
//left | |
new Vector3(-1,0,1), | |
new Vector3(-1,0,-1), | |
new Vector3(-1 - strech + left,2 + up,1 + strech + forward), | |
new Vector3(-1 - strech + left,2 + up,-1 - strech + forward), | |
//right | |
new Vector3(1,0,1), | |
new Vector3(1,0,-1), | |
new Vector3(1 + strech + left,2 + up,1 + strech + forward), | |
new Vector3(1 + strech + left,2 + up,-1 - strech + forward), | |
//front | |
new Vector3(1,0,-1), | |
new Vector3(-1,0,-1), | |
new Vector3(1 + strech + left,2 + up,-1 - strech + forward), | |
new Vector3(-1 - strech + left,2 + up,-1 - strech + forward), | |
//back | |
new Vector3(-1,0,1), | |
new Vector3(1,0,1), | |
new Vector3(-1 - strech + left,2 + up,1 + strech + forward), | |
new Vector3(1 + strech + left,2 + up,1 + strech + forward) | |
}; | |
} | |
// Update is called once per frame | |
void Update () { | |
//Walking | |
var input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); | |
var inputMagnitude = Mathf.Clamp(input.magnitude, 0, 1); | |
transform.position += Time.deltaTime * input * 20f; | |
if (inputMagnitude > 0) | |
if (Vector3.Angle(input.normalized, Vector3.back) > 1) | |
transform.rotation = Quaternion.FromToRotation(Vector3.forward, input.normalized); | |
else | |
transform.rotation = Quaternion.AngleAxis(180, Vector3.up); | |
//Animation | |
Mesh.vertices = GenerateVerticies(Mathf.Sin(Time.realtimeSinceStartup * UpDownSpeed) * UpDownFactor, | |
Mathf.Sin((Time.realtimeSinceStartup + LeftOffset) * LeftSpeed) * LeftFactor, | |
Mathf.Sin(Time.realtimeSinceStartup * StrechSpeed) * StrechFactor, | |
inputMagnitude); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment