Created
October 20, 2020 19:06
-
-
Save SiarheiPilat/324d074585eecb56ab5ac306824b8ae0 to your computer and use it in GitHub Desktop.
Object shaker utilising a coroutine. Suitable for shaking all kinds of objects, but also VERY suitable for camera shake effect.
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 System.Collections; | |
using UnityEngine; | |
// Taken from: https://stackoverflow.com/questions/39380901/shake-an-object-back-and-forth-with-easing-at-the-end | |
public class ObjectShaker : MonoBehaviour | |
{ | |
public GameObject GameObjectToShake; | |
bool shaking = false; | |
IEnumerator shakeGameObjectCOR(GameObject objectToShake, float totalShakeDuration, float decreasePoint, bool objectIs2D = false) | |
{ | |
if (decreasePoint >= totalShakeDuration) | |
{ | |
Debug.LogError("decreasePoint must be less than totalShakeDuration...Exiting"); | |
yield break; //Exit! | |
} | |
//Get Original Pos and rot | |
Transform objTransform = objectToShake.transform; | |
Vector3 defaultPos = objTransform.position; | |
Quaternion defaultRot = objTransform.rotation; | |
float counter = 0f; | |
//Shake Speed | |
const float speed = 0.1f; | |
//Angle Rotation(Optional) | |
const float angleRot = 4; | |
//Do the actual shaking | |
while (counter < totalShakeDuration) | |
{ | |
counter += Time.deltaTime; | |
float decreaseSpeed = speed; | |
float decreaseAngle = angleRot; | |
//Shake GameObject | |
if (objectIs2D) | |
{ | |
//Don't Translate the Z Axis if 2D Object | |
Vector3 tempPos = defaultPos + UnityEngine.Random.insideUnitSphere * decreaseSpeed; | |
tempPos.z = defaultPos.z; | |
objTransform.position = tempPos; | |
//Only Rotate the Z axis if 2D | |
objTransform.rotation = defaultRot * Quaternion.AngleAxis(UnityEngine.Random.Range(-angleRot, angleRot), new Vector3(0f, 0f, 1f)); | |
} | |
else | |
{ | |
objTransform.position = defaultPos + UnityEngine.Random.insideUnitSphere * decreaseSpeed; | |
objTransform.rotation = defaultRot * Quaternion.AngleAxis(UnityEngine.Random.Range(-angleRot, angleRot), new Vector3(1f, 1f, 1f)); | |
} | |
yield return null; | |
//Check if we have reached the decreasePoint then start decreasing decreaseSpeed value | |
if (counter >= decreasePoint) | |
{ | |
Debug.Log("Decreasing shake"); | |
//Reset counter to 0 | |
counter = 0f; | |
while (counter <= decreasePoint) | |
{ | |
counter += Time.deltaTime; | |
decreaseSpeed = Mathf.Lerp(speed, 0, counter / decreasePoint); | |
decreaseAngle = Mathf.Lerp(angleRot, 0, counter / decreasePoint); | |
Debug.Log("Decrease Value: " + decreaseSpeed); | |
//Shake GameObject | |
if (objectIs2D) | |
{ | |
//Don't Translate the Z Axis if 2D Object | |
Vector3 tempPos = defaultPos + UnityEngine.Random.insideUnitSphere * decreaseSpeed; | |
tempPos.z = defaultPos.z; | |
objTransform.position = tempPos; | |
//Only Rotate the Z axis if 2D | |
objTransform.rotation = defaultRot * Quaternion.AngleAxis(UnityEngine.Random.Range(-decreaseAngle, decreaseAngle), new Vector3(0f, 0f, 1f)); | |
} | |
else | |
{ | |
objTransform.position = defaultPos + UnityEngine.Random.insideUnitSphere * decreaseSpeed; | |
objTransform.rotation = defaultRot * Quaternion.AngleAxis(UnityEngine.Random.Range(-decreaseAngle, decreaseAngle), new Vector3(1f, 1f, 1f)); | |
} | |
yield return null; | |
} | |
//Break from the outer loop | |
break; | |
} | |
} | |
objTransform.position = defaultPos; //Reset to original postion | |
objTransform.rotation = defaultRot;//Reset to original rotation | |
shaking = false; //So that we can call this function next time | |
Debug.Log("Done!"); | |
} | |
void shakeGameObject(GameObject objectToShake, float shakeDuration, float decreasePoint, bool objectIs2D = false) | |
{ | |
if (shaking) | |
{ | |
return; | |
} | |
shaking = true; | |
StartCoroutine(shakeGameObjectCOR(objectToShake, shakeDuration, decreasePoint, objectIs2D)); | |
} | |
void Start() | |
{ | |
shakeGameObject(GameObjectToShake, 5, 3f, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment