Skip to content

Instantly share code, notes, and snippets.

@FVSHaLuan
Last active August 29, 2015 14:27
Show Gist options
  • Save FVSHaLuan/c67f801c82f8d79892c1 to your computer and use it in GitHub Desktop.
Save FVSHaLuan/c67f801c82f8d79892c1 to your computer and use it in GitHub Desktop.
Vibrate the camera this component is attached with. Call it from other component (attached to the same camera): GetComponent<CameraVibrator>().Vibrate();
using UnityEngine;
using System.Collections;
/* by FVS - HaLuan */
public class CameraVibrator : MonoBehaviour
{
new Camera camera;
[SerializeField]
float duration = 2.0f;
[SerializeField]
float amplitudeMax = 0.5f;
float originalY;
float amplitude = 0;
float phase = 0;
float angularSpeed = 90;
float phaseInitial = 90;
float amplitudeRestoreSpeed = 1f;
void Awake()
{
///
camera = GetComponent<Camera>();
///
amplitudeRestoreSpeed = amplitudeMax / duration;
///
return;
}
public void Vibrate()
{
///
amplitude = amplitudeMax;
phase = phaseInitial;
originalY = camera.transform.position.y;
}
// Update is called once per frame
void Update()
{
///
if (amplitude == 0)
{
///
enabled = false;
///
return;
}
///
amplitude -= amplitudeRestoreSpeed * Time.deltaTime;
if (amplitude < 0)
amplitude = 0;
///
phase += angularSpeed * Time.deltaTime;
///
float y = originalY + Mathf.Cos(phase) * amplitude;
///
var p = camera.transform.position;
p.y = y;
camera.transform.position = p;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment