Skip to content

Instantly share code, notes, and snippets.

@DomDomHaas
DomDomHaas / UnityShortcuts
Last active August 29, 2015 14:00
Screencapturing & Coding shortcuts for Unity Engine
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
public static class UnityShortcuts
{
@DomDomHaas
DomDomHaas / Unity3D isWallInFront ()
Last active August 29, 2015 14:01
Untiy raycasting in to ensure a character doesn't move inside a wall
if (!isWallInFront (moveDirection)) {
this.rigidbody.MovePosition (this.rigidbody.position + this.transform.right * xAxis * xFacing);
}
private bool isWallInFront (Vector3 direction)
{
if (UnityShortcuts.castRay (this.rigidbody.position, this.transform.right, rayCastLength, moveAlignLayer)) {
//Debug.Log ("got wall hit!");
@DomDomHaas
DomDomHaas / weaponSwitch
Last active August 29, 2015 14:01
Unity Weapon switch with Bone Connection
// copy & past from see: https://www.youtube.com/watch?v=VH0ZfEv_ouc
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//attach 2 weapon prefabs to weapon1 and weapon2
//attach a bone Transform (head, foot, hand etc..) from your
@DomDomHaas
DomDomHaas / gist:a1cfc0ca25d6751e5f56
Last active August 29, 2015 14:01
Coroutines for (gamepad) vibration patterns using iTween to get the linear transition
public IEnumerator ImpactVibration (float enemyDirection)
{
float vibTime = 0.5f;
float impVibStart = 0.1f;
float impVibStrength = 0.35f;
doVibrate = true;
StartCoroutine (DoVibrate ());
if (enemyDirection > 0) {
StartCoroutine (setVibrate (0, vibTime, impVibStart, impVibStrength, "setLeftVibrate"));
@DomDomHaas
DomDomHaas / gist:c61d08bba0d75d84a4da
Last active August 29, 2015 14:01
MasterAudio get PlayListSource change pitch
private AudioSource playlistSource;
playlistCont = PlayListController.Instance;
playlistSource = playlistCont.CurrentPlaylistSource;
if (usePitchAddtion) {
checkAddPitch ();
} else {
checkPitchChange ();
@DomDomHaas
DomDomHaas / gist:f73cb01c03c9ee23bf41
Last active August 29, 2015 14:01
AnimationEvents in Unity
using UnityEngine;
using System.Collections;
public class EnemyAnimationControl : MonoBehaviour
{
private Animator animControl;
private GameObject attackCollider;
private GameObject checkCollider;
@DomDomHaas
DomDomHaas / gist:bf1094ca7d0314165966
Created June 7, 2014 14:09
Unity Robust Data Validation
using UnityEngine;
using System.Collections;
// script according to the talk at unit2013 : https://www.youtube.com/watch?v=Ozc_hXzp_KU
public class ValidateMe05 : MonoBehaviour
{
private const float minFloatValue = 0.1f;
private const float maxFloatValue = 15f;
@DomDomHaas
DomDomHaas / GetSpriteFromPath
Last active August 29, 2015 14:04
get a Sprite from any path using unity WWW Class (and get the mesh to add it on a meshCollider)
/// <summary>
/// Gets a sprite from any path
/// </summary>
/// <returns>The sprite from WW.</returns>
/// <param name="fullPath">Full path.</param>
public static Sprite getSpriteFromWWW (string fullPath, bool packTight = false)
{
fullPath = "file:///" + fullPath;
WWW wwwLoader = new WWW (fullPath);
//Debug.Log ("loading texture " + fullPath + " via www, loaded: " + www);
@DomDomHaas
DomDomHaas / gist:ea3c2b8f5cdb0c66ee91
Created August 1, 2014 23:29
get the average RGB form a texture
private Color getAverageRGB (Texture2D tex)
{
Color[] allPixels = tex.GetPixels (0, 0, tex.width, tex.height);
float r = 0;
float g = 0;
float b = 0;
int interationSteps = 1;
for (int i = 0; i < allPixels.Length; i += interationSteps) {
@DomDomHaas
DomDomHaas / gist:e31156e8c67dbcad8637
Last active August 29, 2015 14:07
Unity RayCast from clicking
Vector3 clickPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
Ray camRay = Camera.main.ScreenPointToRay (clickPos);
RaycastHit hit;
if (Physics.Raycast (camRay.origin, camRay.direction,
out hit, rayLength, fieldMask)) {
// do awesome stuff here
}