Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity:Animation:Shake object vibrate
using UnityEngine;
using System.Collections;
/// http://www.mikedoesweb.com/2012/camera-shake-in-unity/
public class ObjectShake : MonoBehaviour {
private Vector3 originPosition;
private Quaternion originRotation;
public float shake_decay = 0.002f;
public float shake_intensity = .3f;
private float temp_shake_intensity = 0;
void OnGUI (){
if (GUI.Button (new Rect (20,40,80,20), "Shake")){
Shake ();
}
}
void Update (){
if (temp_shake_intensity > 0){
transform.position = originPosition + Random.insideUnitSphere * temp_shake_intensity;
transform.rotation = new Quaternion(
originRotation.x + Random.Range (-temp_shake_intensity,temp_shake_intensity) * .2f,
originRotation.y + Random.Range (-temp_shake_intensity,temp_shake_intensity) * .2f,
originRotation.z + Random.Range (-temp_shake_intensity,temp_shake_intensity) * .2f,
originRotation.w + Random.Range (-temp_shake_intensity,temp_shake_intensity) * .2f);
temp_shake_intensity -= shake_decay;
}
}
void Shake(){
originPosition = transform.position;
originRotation = transform.rotation;
temp_shake_intensity = shake_intensity;
}
}
@musabbir-mamun
Copy link

musabbir-mamun commented Jul 11, 2018

Awesome. Works perfectly.
Thanks for such cool share.

@RedCoreTimber
Copy link

RedCoreTimber commented May 29, 2020

Thanks bro!

@SpiceLag
Copy link

SpiceLag commented Aug 25, 2021

Thank you! Excelent code!
But I discovered that if you attach the Shake() method to a object, in my proyect to a GUI Button, if you click too much, the object changes it's original position, so I put a bool isShaking for controlling when it's shaking or not, and maybe if someone needs it, here is the code! :

using UnityEngine;
using System.Collections;

public class ObjectShake : MonoBehaviour
{
    private Vector3 originPosition;
    private Quaternion originRotation;
    public float shake_decay = 0.003f;
    public float shake_intensity = .2f;
    public bool isShaking = false;

    private float temp_shake_intensity = 0;

    void Update()
    {
        if (temp_shake_intensity > 0 && isShaking == true)
        {
            transform.position = originPosition + Random.insideUnitSphere * temp_shake_intensity;
            transform.rotation = new Quaternion(
                originRotation.x + Random.Range(-temp_shake_intensity, temp_shake_intensity) * .2f,
                originRotation.y + Random.Range(-temp_shake_intensity, temp_shake_intensity) * .2f,
                originRotation.z + Random.Range(-temp_shake_intensity, temp_shake_intensity) * .2f,
                originRotation.w + Random.Range(-temp_shake_intensity, temp_shake_intensity) * .2f);
            temp_shake_intensity -= shake_decay;
        }
        else
        {
            isShaking = false;
        }
    }

    public void Shake()
    {
        if (!isShaking)
        {
            isShaking = true;
            originPosition = transform.position;
            originRotation = transform.rotation;
            temp_shake_intensity = shake_intensity;
        }
    }
}

@GuardianOfGods
Copy link

GuardianOfGods commented Nov 24, 2021

Thanks a lots for this code. Nice Bro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment