You've already forked taptap2024_GJ_chidouren
init
This commit is contained in:
65
Assets/Scripts/Game/Component/ObjectShake.cs
Normal file
65
Assets/Scripts/Game/Component/ObjectShake.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Game.Component
|
||||
{
|
||||
public class ObjectShake : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private ObjectShake LinkObjectShake;
|
||||
private bool startShake = false; //camera是否开始震动
|
||||
private float seconds = 0f; //震动持续秒数
|
||||
private bool started = false; //是否已经开始震动
|
||||
private float quake = 0.2f; //震动系数
|
||||
|
||||
[SerializeField] private float quakeOffset = 1;
|
||||
|
||||
private Vector3 startPOS; //camera的起始位置
|
||||
|
||||
// Use this for initialization
|
||||
void OnEnable()
|
||||
{
|
||||
this.startPOS = transform.localPosition;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void LateUpdate()
|
||||
{
|
||||
if (startShake)
|
||||
{
|
||||
transform.localPosition = this.startPOS + Random.insideUnitSphere * (this.quake * this.quakeOffset);
|
||||
}
|
||||
|
||||
if (started)
|
||||
{
|
||||
StartCoroutine(WaitForSecond(seconds));
|
||||
started = false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 外部调用控制camera震动
|
||||
/// </summary>
|
||||
/// <param name="duration">震动时间</param>
|
||||
/// <param name="scale">震动幅度</param>
|
||||
public void ShakeFor(float duration, float scale)
|
||||
{
|
||||
// if (startShake)
|
||||
// return;
|
||||
seconds = duration;
|
||||
started = true;
|
||||
startShake = true;
|
||||
quake = scale;
|
||||
if (!ReferenceEquals (this.LinkObjectShake , null))
|
||||
{
|
||||
this.LinkObjectShake.ShakeFor (duration , scale);
|
||||
}
|
||||
}
|
||||
IEnumerator WaitForSecond(float a)
|
||||
{
|
||||
// camPOS = transform.position;
|
||||
|
||||
yield return new WaitForSeconds(a);
|
||||
startShake = false;
|
||||
transform.localPosition = this.startPOS;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user