├── Scripts ├── TimeFreezer.cs ├── SpriteColorFlasher.cs └── CameraShaker.cs ├── README.md ├── LICENSE └── .gitignore /Scripts/TimeFreezer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class TimeFreezer : MonoBehaviour 6 | { 7 | public void FreezeTime(float duration) 8 | { 9 | StopAllCoroutines(); 10 | StartCoroutine(DoTimeFreeze(duration)); 11 | } 12 | 13 | private IEnumerator DoTimeFreeze(float duration) 14 | { 15 | Time.timeScale = 0; 16 | yield return new WaitForSecondsRealtime(duration); 17 | Time.timeScale = 1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Scripts/SpriteColorFlasher.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SpriteColorFlasher : MonoBehaviour 6 | { 7 | public void FlashColor(SpriteRenderer spriteRend, float duration, Color color) { 8 | //TODO: Safety check that are sprite isn't already in use? 9 | StartCoroutine(DoColorFlash(spriteRend, duration, color)); 10 | } 11 | 12 | private IEnumerator DoColorFlash(SpriteRenderer spriteRend, float duration, Color newColor) { 13 | Color oldColor = spriteRend.color; 14 | spriteRend.color = newColor; 15 | yield return new WaitForSeconds(duration); 16 | //Check to ensure the sprite's game object hasn't been destroyed 17 | if(spriteRend != null) { 18 | spriteRend.color = oldColor; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 💥Enhancing your Games Feel in Unity 2 | A collection of scripts to enhance the feel of your games 3 | 4 | ### 1) Camera Shaker 5 | This is a drag & drop script that shakes your camera with a single function. It's all done using the Cinemachine Unity package. 6 | 7 | **Step-by-step Guide** 8 | 1. In Unity, install the Cinemachine package from the Package Manager window. 9 | 2. Right-click in the hierarchy and, under Cinemachine, create a new Virtual Camera (or 2D Camera) 10 | 3. Near the bottom of the Cinemachine Virtual Camera component find Noise. 11 | Set this to Basic Multi Channel Perlin 12 | 4. Finally, add the Camera Shaker script to an object in the scene 13 | 14 | That's everything! Call the Basic or Smooth Shake function whenever you want some Camera Shake. 15 | 16 | ### 2) Sprite Color Changer 17 | A script to make a spite flash! Designed for hit flashes to show when a player or enemy takes damage. 18 | 19 | ### 3) Time Freezer 20 | Used to freeze time for a short duration. Can be used to further emphasise events in game. eg: enemy's death 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Dawnosaur 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # This .gitignore file should be placed at the root of your Unity project directory 2 | # 3 | # Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore 4 | # 5 | /[Ll]ibrary/ 6 | /[Tt]emp/ 7 | /[Oo]bj/ 8 | /[Bb]uild/ 9 | /[Bb]uilds/ 10 | /[Ll]ogs/ 11 | /[Mm]emoryCaptures/ 12 | 13 | # Asset meta data should only be ignored when the corresponding asset is also ignored 14 | !/[Aa]ssets/**/*.meta 15 | 16 | # Uncomment this line if you wish to ignore the asset store tools plugin 17 | # /[Aa]ssets/AssetStoreTools* 18 | 19 | # Autogenerated Jetbrains Rider plugin 20 | [Aa]ssets/Plugins/Editor/JetBrains* 21 | 22 | # Visual Studio cache directory 23 | .vs/ 24 | 25 | # Gradle cache directory 26 | .gradle/ 27 | 28 | # Autogenerated VS/MD/Consulo solution and project files 29 | ExportedObj/ 30 | .consulo/ 31 | *.csproj 32 | *.unityproj 33 | *.sln 34 | *.suo 35 | *.tmp 36 | *.user 37 | *.userprefs 38 | *.pidb 39 | *.booproj 40 | *.svd 41 | *.pdb 42 | *.mdb 43 | *.opendb 44 | *.VC.db 45 | 46 | # Unity3D generated meta files 47 | *.pidb.meta 48 | *.pdb.meta 49 | *.mdb.meta 50 | 51 | # Unity3D generated file on crash reports 52 | sysinfo.txt 53 | 54 | # Builds 55 | *.apk 56 | *.unitypackage 57 | 58 | # Crashlytics generated file 59 | crashlytics-build.properties 60 | 61 | -------------------------------------------------------------------------------- /Scripts/CameraShaker.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using Cinemachine; 5 | 6 | public class CameraShaker : MonoBehaviour 7 | { 8 | private CinemachineVirtualCamera _virtualCamera; 9 | private CinemachineBasicMultiChannelPerlin _cameraNoise; 10 | 11 | private const float SHAKE_STRENGTH = 1.0f; 12 | 13 | private void Start() { 14 | _virtualCamera = FindObjectOfType(); 15 | _cameraNoise = _virtualCamera.GetCinemachineComponent(); 16 | } 17 | 18 | #region Basic Shake 19 | public void BasicShake(float intensity, float duration) { 20 | StopAllCoroutines(); 21 | StartCoroutine(DoBasicShake(intensity, duration)); 22 | } 23 | 24 | private IEnumerator DoBasicShake(float intensity, float duration) { 25 | _cameraNoise.m_AmplitudeGain = intensity * SHAKE_STRENGTH; 26 | yield return new WaitForSeconds(duration); 27 | _cameraNoise.m_AmplitudeGain = 0; 28 | } 29 | #endregion 30 | 31 | #region Smooth Shake 32 | public void SmoothShake(float intensity, float duration, float fadeInTime = 0, float fadeOutTime = 0) { 33 | StopAllCoroutines(); 34 | StartCoroutine(DoSmoothShake(intensity, duration, fadeInTime, fadeOutTime)); 35 | } 36 | 37 | private IEnumerator DoSmoothShake(float intensity, float duration, float fadeInTime, float fadeOutTime) { 38 | float startTime = Time.time; 39 | _cameraNoise.m_AmplitudeGain = 0; 40 | 41 | //Fade In 42 | while(Time.time - startTime < fadeInTime) { 43 | float fadeAmount = Mathf.Lerp(0, intensity, (Time.time - startTime) / fadeInTime); 44 | _cameraNoise.m_AmplitudeGain = fadeAmount * intensity * SHAKE_STRENGTH; 45 | yield return null; 46 | } 47 | 48 | //Main Shake 49 | _cameraNoise.m_AmplitudeGain = intensity * SHAKE_STRENGTH; 50 | yield return new WaitForSeconds(duration); 51 | 52 | //Fade Out 53 | while (Time.time - startTime < fadeInTime + duration + fadeOutTime) 54 | { 55 | float fadeAmount = Mathf.Lerp(intensity, 0, (Time.time - (startTime + fadeInTime + duration)) / fadeInTime); 56 | _cameraNoise.m_AmplitudeGain = fadeAmount * intensity * SHAKE_STRENGTH; 57 | yield return null; 58 | } 59 | 60 | _cameraNoise.m_AmplitudeGain = 0; 61 | } 62 | #endregion 63 | } 64 | --------------------------------------------------------------------------------