-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraShake.cs
More file actions
40 lines (33 loc) · 1.34 KB
/
CameraShake.cs
File metadata and controls
40 lines (33 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour {
Vector3 cameraInitialPosition;
public float shakeMagnetude;//0.05f, 0.5f
public Camera mainCamera;
public bool active;
//Shakes the camera, based on a magnitude and for a shakeTime.
public void ShakeIt(float shakeMag, float shakeTime)
{
cameraInitialPosition = mainCamera.transform.localPosition;
shakeMagnetude = shakeMag;
InvokeRepeating ("StartCameraShaking", 0f, 0.002f);
Invoke ("StopCameraShaking", shakeTime);
}
//Used by ShakeIt() to start shaking the camera.
void StartCameraShaking()
{
float cameraShakingOffsetX = Random.value * shakeMagnetude * 2 - shakeMagnetude;
float cameraShakingOffsetY = Random.value * shakeMagnetude * 2 - shakeMagnetude;
Vector3 cameraIntermadiatePosition = mainCamera.transform.localPosition;
cameraIntermadiatePosition.x += cameraShakingOffsetX;
cameraIntermadiatePosition.y += cameraShakingOffsetY;
mainCamera.transform.localPosition = Vector3.Lerp(mainCamera.transform.localPosition,cameraIntermadiatePosition, 0.2f);
}
//Used by ShakeIt() to stop shaking the camera.
void StopCameraShaking()
{
CancelInvoke ("StartCameraShaking");
mainCamera.transform.localPosition = new Vector3(0,0,0);
}
}