2015-07-13 15 views

Trả lời

11

Thảo luận tuyệt vời về phát hiện "lắc" có thể được tìm thấy trong this thread trên diễn đàn Unity.

Từ bài Brady của:

Từ những gì tôi có thể nói trong một số ứng dụng mẫu iPhone của Apple, về cơ bản bạn chỉ cần thiết lập một ngưỡng vector cường độ, thiết lập một bộ lọc thông cao trên các giá trị gia tốc, sau đó nếu cường độ của vectơ tăng tốc đó dài hơn ngưỡng đặt của bạn, nó được coi là "lắc". đang

jmpp của đề nghị (sửa đổi để có thể đọc và được gần gũi hơn với giá trị C#):

float accelerometerUpdateInterval = 1.0f/60.0f; 
// The greater the value of LowPassKernelWidthInSeconds, the slower the 
// filtered value will converge towards current input sample (and vice versa). 
float lowPassKernelWidthInSeconds = 1.0f; 
// This next parameter is initialized to 2.0 per Apple's recommendation, 
// or at least according to Brady! ;) 
float shakeDetectionThreshold = 2.0f; 

float lowPassFilterFactor; 
Vector3 lowPassValue; 

void Start() 
{ 
    lowPassFilterFactor = accelerometerUpdateInterval/lowPassKernelWidthInSeconds; 
    shakeDetectionThreshold *= shakeDetectionThreshold; 
    lowPassValue = Input.acceleration; 
} 

void Update() 
{ 
    Vector3 acceleration = Input.acceleration; 
    lowPassValue = Vector3.Lerp(lowPassValue, acceleration, lowPassFilterFactor); 
    Vector3 deltaAcceleration = acceleration - lowPassValue; 

    if (deltaAcceleration.sqrMagnitude >= shakeDetectionThreshold) 
    { 
     // Perform your "shaking actions" here. If necessary, add suitable 
     // guards in the if check above to avoid redundant handling during 
     // the same shake (e.g. a minimum refractory period). 
     Debug.Log("Shake event detected at time "+Time.time); 
    } 
} 

Lưu ý: Tôi khuyên bạn nên đọc toàn bộ chủ đề cho bối cảnh đầy đủ.

Các vấn đề liên quan