Advertisement
LittleAngel

Aligning

Feb 17th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. // Reference to the GameObject
  2. // Assuming that this transform will rotate on the z
  3. public GameObject obj;
  4. //An alignment speed to control the lerp
  5. public float alignmentSpeed;
  6. // Public number for ease of coding
  7. // Is this 3?? 360/130 = 3?
  8. // This would make the increments at 0/360*, 120* & 240*
  9. public float numberOfIncrements = 3;
  10. // Internal number for ease of coding
  11. private float increment = 1.0f/numberOfIncrements;
  12.  
  13. // In whatever code that detects input:
  14. void SomeMethodDetectingInput () {
  15. int newLocation = 0;
  16. int normalizedLocation = thisGameObject.transform.rotation.z/360;
  17. if (Tap || Release || Release_Off)
  18. StartCoroutine ("AlignToLocation");
  19. if (Drag)
  20. StopCoroutine ("AlignToLocation");
  21. }
  22.  
  23. // Co-routine for animating the "snap" to the current location when the user releases
  24. IEnumerator AlignToLocation () {
  25. // Create and Set all of the current values in preparation of "lerping"
  26. bool aligning = true;
  27. // Create a "start time" to control the lerp
  28. float startTime = Time.time;
  29. // Create current location (normalized)
  30. float currentLocation = (obj.transform.rotation/360);
  31. // Find closest final location
  32. float closestLocation = increment * (Mathf.Round((obj.transform.rotation/360)/increment));
  33.  
  34. // While aligning: Lerp the GameObject location to the next closest chapter
  35. while (aligning) {
  36. (obj.transform.rotation/360) = Mathf.Lerp (currentLocation, closestLocation, (Time.time - startTime) * alignmentSpeed);
  37. if (Mathf.Abs (closestLocation - (obj.transform.rotation/360)) < threshold) {
  38. (obj.transform.rotation/360) = closestLocation;
  39. aligning = false;
  40. }
  41. yield return null;
  42. }
  43. // Enable code here for triggering "something" AFTER the knob has settled
  44. DoSomething (closestLocation);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement