Last active
August 7, 2023 18:34
-
-
Save GuilleUCM/d882e228d93c7f7d0820 to your computer and use it in GitHub Desktop.
Unity:Animation:Shake object vibrate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
thanks much - worked perfectly!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lots for this code. Nice Bro!