Unity教程之-Unity3d实现屏幕抖动效果

很多游戏玩家游戏死亡时一般都需要屏幕抖一下下或者屏幕变灰。为了在unity3d中实现这个特效,百度了下相关写法,发现方法很多~~~,找来找去,找到个简单粗暴地,啥都不需要,Camera Viewpoint实现 一个脚本拖动到Camera上就可以了,略微修改了一点点,share一下,下面是代码

?
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using UnityEngine;
using System.Collections;
 
public class ShakeCamera : MonoBehaviour
{
 
// 震动标志位
private bool isshakeCamera = false ;
 
// 震动幅度
public float shakeLevel = 3f;
// 震动时间
public float setShakeTime = 0.2f;
// 震动的FPS
public float shakeFps = 45f;
 
private float fps;
private float shakeTime = 0.0f;
private float frameTime = 0.0f;
private float shakeDelta = 0.005f;
private Camera selfCamera;
 
void Awake()
{
selfCamera = GetComponent();
}
 
// Use this for initialization
void Start()
{
shakeTime = setShakeTime;
fps = shakeFps;
frameTime = 0.03f;
shakeDelta = 0.005f;
}
 
// Update is called once per frame
void Update()
{
if (isshakeCamera)
{
if (shakeTime > 0)
{
shakeTime -= Time.deltaTime;
if (shakeTime <= 0)
{
selfCamera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
isshakeCamera = false ;
shakeTime = setShakeTime;
fps = shakeFps;
frameTime = 0.03f;
shakeDelta = 0.005f;
}
else
{
frameTime += Time.deltaTime;
 
if (frameTime > 1.0 / fps)
{
frameTime = 0;
selfCamera.rect = new Rect(shakeDelta * (-1.0f + shakeLevel * Random.value), shakeDelta * (-1.0f + shakeLevel * Random.value), 1.0f, 1.0f);
}
}
}
}
}
 
public void shake()
{
isshakeCamera = true ;
}
}
 
  

好了本篇unity3d教程到此结束,下篇我们再会!

你可能感兴趣的:(Unity3D)