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 bro!
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;
}
}
}
Thanks a lots for this code. Nice Bro!
thanks much - worked perfectly!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome. Works perfectly.
Thanks for such cool share.