在一些格斗、射击以及动作类游戏中
相机震动效果是十分重要的
一个平凡的镜头
在关键时刻加入相机震动后
便可以展现出碰撞、危险、打斗以及激动人心的效果
相机震动的实现方式有很多
但都会涉及摄像机位置的变化
using System.Collections;
using UnityEngine; public class CameraShake : MonoBehaviour {
private Transform ThisTransform = null;
public float ShakeTime = 2.0f;
public float ShakeAmount = 3.0f;
public float ShakeSpeed = 2.0f;
// Use this for initialization
void Start () {
ThisTransform = GetComponent<Transform>();
} public IEnumerator Shake() {
Vector3 OrigPosition = ThisTransform.localPosition;
float ElapsedTime = 0.0f;
while (ElapsedTime < ShakeTime) {
Vector3 RandomPoint = OrigPosition + Random.insideUnitSphere * ShakeAmount;
ThisTransform.localPosition = Vector3.Lerp(ThisTransform.localPosition,RandomPoint,Time.deltaTime*ShakeSpeed);
yield return null;
ElapsedTime += Time.deltaTime;
}
ThisTransform.localPosition = OrigPosition;
}
}
使用时
将此脚本挂到相机上
然后在需要的时候开启协程即可
StartCoroutine(Shake());