我正在Unity3d上开发一款游戏。这里我有5张牌,我正试图用抖动移动来洗牌,但根据我的代码,在执行抖动时,牌正在洗牌,我需要该功能可以延迟或在抖动结束时启动。这是我的代码:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Shake1 : MonoBehaviour {
int numTimes = 50;
public Image personajes;
public Image lugares;
public Image situaciones;
public Image emociones;
public Image objetos;
public GameObject camShake;
float accelerometerUpdateInterval;
// The greater the value of LowPassKernelWidthInSeconds, the slower the filtered value will converge towards current input sample (and vice versa).
float lowPassKernelWidthInSeconds;
// This next parameter is initialized to 2.0 per Apple's recommendation, or at least according to Brady! ;)
float shakeDetectionThreshold ;
float lowPassFilterFactor;
Vector3 lowPassValue;
Vector3 acceleration;
Vector3 deltaAcceleration;
void Start () {
lowPassKernelWidthInSeconds = 1.0f;
accelerometerUpdateInterval = 1.0f / 60.0f;
lowPassValue = Vector3.zero;
lowPassFilterFactor = accelerometerUpdateInterval / lowPassKernelWidthInSeconds;
}
public Vector3 LowPassFilter(Vector3 newSample){
lowPassValue = Vector3.Lerp (lowPassValue, newSample,lowPassFilterFactor);
return lowPassValue;
}
void Update () {
acceleration = Input.acceleration;
deltaAcceleration = acceleration - LowPassFilter (acceleration);
if (Mathf.Abs (deltaAcceleration.x) >= 0.2 && Mathf.Abs (deltaAcceleration.y) >= 0.2 && Mathf.Abs (deltaAcceleration.z) >= 0.2) {
for (int i = 0; i < numTimes; i++) {
Handheld.Vibrate();
if (personajes.transform.parent.name == "Panel_Superior") {
personajes.sprite = Resources.Load<Sprite> ("Cartas/pillow-cards-" + Random.Range (2, 13)) as Sprite;
}
if (situaciones.transform.parent.name == "Panel_Superior") {
situaciones.sprite = Resources.Load<Sprite> ("Cartas/pillow-cards-" + Random.Range (28, 39)) as Sprite;
}
if (emociones.transform.parent.name == "Panel_Superior") {
emociones.sprite = Resources.Load<Sprite> ("Cartas/pillow-cards-" + Random.Range (41, 52)) as Sprite;
}
if (lugares.transform.parent.name == "Panel_Superior") {
lugares.sprite = Resources.Load<Sprite> ("Cartas/pillow-cards-" + Random.Range (15, 26)) as Sprite;
}
if (objetos.transform.parent.name == "Panel_Superior") {
objetos.sprite = Resources.Load<Sprite> ("Cartas/pillow-cards-" + Random.Range (54, 68)) as Sprite;
}
}
}
}
}
当Update()
在60Hz时被调用时,您可以创建一个计数器(假设它被称为zeroAcc
),每当加速度低于"抖动"的触发阈值时,该计数器就会增加
在触发抖动的同时,可以存储此时zeroAcc
的值,比如accTri
现在您需要决定延迟,在本例中可能是30次Update()
调用,然后触发回调
它可能看起来像这样:
void Update(){
//keep your code you already have and add this:
if (zeroAcc - accTri > 30) {
callback ();
accTri = int.MaxValue; //we don't want to trigger again
}
}