using System.Collections; using System.Collections.Generic; using UnityEngine; //思想:在短时间内在规定圆内随机震动对象位置,从而实现震动效果 public class CamZhengDong : MonoBehaviour { //震动对象属性 private Transform Tr = null; // public float zhenDongShiJian = 6.0f; //震动幅度 public float fuDu = 10.0f; //震动对象移动速度 public float speed = 2.0f; // Use this for initialization void Start () { Tr = GetComponent<Transform>(); StartCoroutine(CamShake()); } //private void Update() //{ // if (ElapsedTime < ShakeTime) { // Vector3 RanPoint = OrigPosition + Random.insideUnitSphere * ShakeAmount; // Tr.localPosition = Vector3.Lerp(Tr.localPosition, RanPoint, Time.deltaTime * speed); // //计时 // ElapsedTime += Time.deltaTime; // } //} public IEnumerator CamShake() { float jishi = 0.0f; Vector3 OrigPosition = Tr.localPosition; while (jishi < zhenDongShiJian) { //Random.insideUnitSphere单位圆内随机数 Vector3 RanPoint = OrigPosition + Random.insideUnitSphere * zhenDongShiJian; Tr.localPosition = Vector3.Lerp(Tr.localPosition,RanPoint,Time.deltaTime * speed); yield return null; //计时 jishi += Time.deltaTime; } } }