Scripts/CameraShake

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "Category: Camera Category: MonoBehaviour Category: C Sharp Category: Unity 5.x Author: Daniel Moore (Firedan1176) ==Description This script offers a simple Cam...")
 
Line 4: Line 4:
 
[[Category: Unity 5.x]]
 
[[Category: Unity 5.x]]
 
Author: Daniel Moore (Firedan1176)
 
Author: Daniel Moore (Firedan1176)
==Description
+
 
 +
 
 +
==Description==
 +
 
 
This script offers a simple Camera Shake effect with a given time and amount. The script does not affect the transform of the main Camera (or any Camera), so this script should work with any Character Controller, even with Unity 5's.
 
This script offers a simple Camera Shake effect with a given time and amount. The script does not affect the transform of the main Camera (or any Camera), so this script should work with any Character Controller, even with Unity 5's.
  
 
==Usage==
 
==Usage==
 +
 
Place this script on a ''parent'' gameobject of the main camera, or any camera. The shake effect can be triggered at any point with the preset values, or you can pass in an amount and a duration, which will be added to the current. To pass values via another script, use:
 
Place this script on a ''parent'' gameobject of the main camera, or any camera. The shake effect can be triggered at any point with the preset values, or you can pass in an amount and a duration, which will be added to the current. To pass values via another script, use:
  

Revision as of 21:44, 26 December 2015

Author: Daniel Moore (Firedan1176)


Contents

Description

This script offers a simple Camera Shake effect with a given time and amount. The script does not affect the transform of the main Camera (or any Camera), so this script should work with any Character Controller, even with Unity 5's.

Usage

Place this script on a parent gameobject of the main camera, or any camera. The shake effect can be triggered at any point with the preset values, or you can pass in an amount and a duration, which will be added to the current. To pass values via another script, use:

GetComponent<CameraShake>().ShakeCamera(20f, 1f);

C# - CameraShake.cs

 0.01f) {
			Vector3 rotationAmount = Random.insideUnitSphere * shakeAmount;//A Vector3 to add to the Local Rotation
			rotationAmount.z = 0;//Don't change the Z; it looks funny.
 
			shakePercentage = shakeDuration / startDuration;//Used to set the amount of shake (% * startAmount).
 
			shakeAmount = startAmount * shakePercentage;//Set the amount of shake (% * startAmount).
			shakeDuration = Mathf.Lerp(shakeDuration, 0, Time.deltaTime);//Lerp the time, so it is less and tapers off towards the end.
 
 
			if(smooth)
				transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(rotationAmount), Time.deltaTime * smoothAmount);
			else
				transform.localRotation = Quaternion.Euler (rotationAmount);//Set the local rotation the be the rotation amount.
 
			yield return null;
		}
		transform.localRotation = Quaternion.identity;//Set the local rotation to 0 when done, just to get rid of any fudging stuff.
		isRunning = false;
	}
 
}


Notes

The duration depends on a Math.Lerp, which will NOT make the time be exact. Setting the duration is not exact, but as long as you're fine with that not being so, it will look nice. You can always just change it to subtract Time.deltaTime, if you prefer.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox