Wednesday, April 22, 2015

How to make camera shake in Unity3D (javascript)

Making the Camera shake in Unity3D can add much needed dramatic effects to your game. To implement you need to manipulate position and rotation in random ranges. See below:

var originPosition:Vector3;
var originRotation:Quaternion;
var shake_decay: float;
var shake_intensity: float;

function ShakeCustom(x:float, y:float){
   originPosition = transform.position;
   originRotation = transform.rotation;
   shake_intensity = x;
   shake_decay = y;

function LateUpdate() {
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;

No comments:

Post a Comment