Unity 实现图片抖动
1 using UnityEngine; 2 using System.Collections; 3 //物体抖动 4 public class ObjectShake : MonoBehaviour { 5 6 public bool isEnable=true; 7 public Vector3 shakedegree=Vector3.one*0.1f; 8 public float addDegreeTime=2; 9 10 private Vector3 originPosition; 11 private float timeCount; 12 13 void Start() 14 { 15 originPosition=transform.localPosition; 16 } 17 18 void Update() 19 { 20 if(isEnable) 21 { 22 if(timeCount<addDegreeTime) 23 timeCount+=Time.deltaTime; 24 else 25 timeCount=addDegreeTime; 26 transform.localPosition=new Vector3(GetX(),GetY(),GetZ()); 27 } 28 else 29 { 30 if(timeCount>0) 31 { 32 timeCount-=Time.deltaTime; 33 transform.localPosition=new Vector3(GetX(),GetY(),GetZ()); 34 } 35 else 36 { 37 transform.localPosition=originPosition; 38 } 39 } 40 } 41 42 private float GetX() 43 { 44 return originPosition.x+Random.Range(-1*timeCount*shakedegree.x,timeCount*shakedegree.x); 45 } 46 private float GetY() 47 { 48 return originPosition.y+Random.Range(-1*timeCount*shakedegree.y,timeCount*shakedegree.y); 49 } 50 private float GetZ() 51 { 52 return originPosition.z+Random.Range(-1*timeCount*shakedegree.y,timeCount*shakedegree.y); 53 } 54 }