jeudi 2 mai 2013

Low cost special effect : Shaking the camera

Ouya isn't powerful.
It's small, it's pretty, it's cheap, and it's very interesting for developers and curious players, but don't expect groundbreaking graphics compared to other consoles. That's not what it is for.

So, when you are afraid of using particles or shaders, you are looking for any low-cost effect possible.

My personnal favorite is shaking the camera.
It's easy, it's simple, and it just works.

So today, I'm offering you my own easy-to-implement camera shaker.
Just attach it to the camera, disable the script, and when you want to shake it, set magnitude, duration, and enable it.

Plus, it tries not to interfere with your camera controller !


using UnityEngine;
using System.Collections;

[AddComponentMenu("Scripts/Camera/Shaker")]
public class Shaker : MonoBehaviour {
public float Magnitude;
public float Duration;
private float CurrentMagnitude;
private float startTime;

private Vector3 OriginalPosition;
private Vector2 ShakerVector=Vector2.one;
private float facteur;
// Update is called once per frame
void OnEnable()
{
startTime=Time.time;
CurrentMagnitude=Magnitude;
facteur=-Magnitude/Duration;
OriginalPosition=transform.position;
}
//Avant de lancer les prochaines updates
void OnPostRender()
{
transform.position=OriginalPosition;
if(CurrentMagnitude<=0) 
{
enabled=false;
}
}

//after the camera controller has done its job
void LateUpdate()
{
CurrentMagnitude = facteur*(Time.time-startTime)+Magnitude;
OriginalPosition=transform.position;
transform.position+=new Vector3(ShakerVector.x*CurrentMagnitude*Random.Range(-1f,1.01f),ShakerVector.y*CurrentMagnitude*Random.Range(-1f,1.01f),0f);
}

public void Shake(float Magnitude,float Duration)
{
this.Magnitude = Magnitude;
this.Duration=Duration;
enabled=true;
}
}


Aucun commentaire:

Enregistrer un commentaire