Skip to content

Instantly share code, notes, and snippets.

@donovankeith
Created April 5, 2017 22:56
Show Gist options
  • Save donovankeith/ac754d657a6ceab3b07ed14270de0b79 to your computer and use it in GitHub Desktop.
Save donovankeith/ac754d657a6ceab3b07ed14270de0b79 to your computer and use it in GitHub Desktop.
Simple Example of Enums and Switch Statements
// PrimitiveMove.cs
// Creates a simple primitive in Unity and moves it in a user-selected driection.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrimitiveMover : MonoBehaviour {
public enum Shape {Box, Ball, Pill};
public enum Direction {Left, Right, Up, Down, Forward, Backward}
private GameObject primitive;
public Shape shape = Shape.Box;
public Direction direction = Direction.Up;
// Use this for initialization
void Start () {
if (shape == Shape.Box) {
primitive = GameObject.CreatePrimitive (PrimitiveType.Cube);
} else if (shape == Shape.Ball) {
primitive = GameObject.CreatePrimitive (PrimitiveType.Sphere);
} else if (shape == Shape.Pill) {
primitive = GameObject.CreatePrimitive (PrimitiveType.Cube);
}
}
// Update is called once per frame
void Update () {
Vector3 moveDirection;
// Switch Case Example
switch (direction){
case Direction.Up:
moveDirection = Vector3.up;
break;
case Direction.Down:
moveDirection = Vector3.down;
break;
case Direction.Forward:
moveDirection = Vector3.forward;
break;
case Direction.Backward:
moveDirection = Vector3.back;
break;
case Direction.Left:
moveDirection = Vector3.left;
break;
case Direction.Right:
moveDirection = Vector3.right;
break;
default:
moveDirection = Vector3.forward;
break;
}
primitive.GetComponent<Transform>().Translate (moveDirection*Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment