我正在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;
}
}
}
}
}
答案 0 :(得分:0)
当Update()
以60Hz调用时,您可以创建一个计数器(让我们称之为zeroAcc
),每当加速度低于触发阈值时,该计数器就会增加&#34;摇&#34 ;.
在触发震动的同时,您可以在此时存储zeroAcc
的值,让我们说accTri
。
现在你需要决定延迟,这个例子可以是Update()
的30次调用,然后触发回调。
它看起来像这样:
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
}
}