Showing posts with label Free. Show all posts
Showing posts with label Free. Show all posts

Unity3D Camera Shake Script

Unity3D Camera Shake Free Script

After much Googling for how to do a camera shaking effect in Unity, I decided to write my own small script to accomplish the effect. It combines both positional movement with rotational movement to simulate closely an actual camera shake.
The two key variables are:

shake_intensity

Shake_intensity determines the initial intensity of the shaking — how much variance to allow in the camera position.

shake_decay

Shake_decay controls is the amount that shake_intensity is decremented each update. It determines if the shake is long or short.

JavaScript
var originPosition:Vector3;
var originRotation:Quaternion;
var shake_decay: float;
var shake_intensity: float;;
 
function OnGUI () {
   if (GUI.Button (Rect (20,40,80,20), "Shake")) {
      Shake();
   }
}
 
function Update(){
   if(shake_intensity > 0){
      transform.position = originPosition + Random.insideUnitSphere * shake_intensity;
      transform.rotation = Quaternion(
      originRotation.x + Random.Range(-shake_intensity,shake_intensity)*.2,
      originRotation.y + Random.Range(-shake_intensity,shake_intensity)*.2,
      originRotation.z + Random.Range(-shake_intensity,shake_intensity)*.2,
      originRotation.w + Random.Range(-shake_intensity,shake_intensity)*.2);
      shake_intensity -= shake_decay;
   }
}
 
function Shake(){
   originPosition = transform.position;
   originRotation = transform.rotation;
   shake_intensity = .3;
   shake_decay = 0.002;
}

C#
using UnityEngine;
using System.Collections;
public class cameraShake : MonoBehaviour
{
   private Vector3 originPosition;
   private Quaternion originRotation;
   public float shake_decay;
   public float shake_intensity;
 
   void OnGUI (){
      if (GUI.Button (new Rect (20,40,80,20), "Shake")){
         Shake ();
      }
   }
 
   void Update (){
      if (shake_intensity > 0){
         transform.position = originPosition + Random.insideUnitSphere * shake_intenity;
         transform.rotation = new Quaternion(
         originRotation.x + Random.Range (-shake_intensity,shake_intensity) * .2f,
         originRotation.y + Random.Range (-shake_intensity,shake_intensity) * .2f,
         originRotation.z + Random.Range (-shake_intensity,shake_intensity) * .2f,
         originRotation.w + Random.Range (-shake_intensity,shake_intensity) * .2f);
         shake_intensity -= shake_decay;
      }
   }
 
   void Shake(){
      originPosition = transform.position;
      originRotation = transform.rotation;
      shake_intensity = .3f;
   shake_decay = 0.002f;
   }
}

Unity3D Camera Shake Free Script

Unity Basic Low Poly Shader

Unity Basic Low Poly Shader

Just starting out with Unity shader programming.

Made this low poly shader:
Unity Basic Low Poly Shader

Code:
Shader "Custom/LowPoly2"
{
 Properties
 {
  _BaseColor ("BaseColor", Color) = (1,1,1,1)
 }
 SubShader
 {
  Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" }
  LOD 100

  Pass
  {
   CGPROGRAM
   #pragma vertex vert
   #pragma geometry geom
   #pragma fragment frag
   
   #include "UnityCG.cginc"
   #include "UnityLightingCommon.cginc"

   struct appdata
   {
    float4 pos : POSITION;
    float3 norm : NORMAL;
   };

   struct v2g
   {
    float4 pos : SV_POSITION;
    float3 norm : TEXCOORD0;
   };

   struct g2f {
    float4 pos : SV_POSITION;
    fixed4 diffColor : COLOR0;
   };
   
   v2g vert (appdata v)
   {
    v2g o;
    o.pos = v.pos;
    o.norm = v.norm;
    return o;
   }

   [maxvertexcount(3)]
   void geom(triangle v2g i[3], inout TriangleStream triStream)
   {
    float3 commonNorm = i[0].norm;
    float4 worldPos = mul(unity_ObjectToWorld, i[0].pos);

    half3 worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, commonNorm));
    half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    fixed4 diff = nl * _LightColor0;

    g2f o;
    o.pos = mul(UNITY_MATRIX_MVP, i[0].pos);
    o.diffColor = diff;
    triStream.Append(o);

    o.pos = mul(UNITY_MATRIX_MVP, i[1].pos);
    o.diffColor = diff;
    triStream.Append(o);

    o.pos = mul(UNITY_MATRIX_MVP, i[2].pos);
    o.diffColor = diff;
    triStream.Append(o);
   }

   uniform float4 _BaseColor;
   fixed4 frag (g2f i) : SV_Target
   {
    fixed4 mainColor = _BaseColor * i.diffColor;
    return mainColor;
   }
   ENDCG
  }
 }
}

Unity Basic Low Poly Shader

Per Character Animation - Free source code

Unity Per Character Animation - Free source code

Low Poly Rocks Pack

Low Poly Rocks Pack - Free Download

Few day ago I posted several screenshots of my rocks and you guys liked it so much. Thanks.