Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Reference to the GameObject
- // Assuming that this transform will rotate on the z
- public GameObject obj;
- //An alignment speed to control the lerp
- public float alignmentSpeed;
- // Public number for ease of coding
- // Is this 3?? 360/130 = 3?
- // This would make the increments at 0/360*, 120* & 240*
- public float numberOfIncrements = 3;
- // Internal number for ease of coding
- private float increment = 1.0f/numberOfIncrements;
- // In whatever code that detects input:
- void SomeMethodDetectingInput () {
- int newLocation = 0;
- int normalizedLocation = thisGameObject.transform.rotation.z/360;
- if (Tap || Release || Release_Off)
- StartCoroutine ("AlignToLocation");
- if (Drag)
- StopCoroutine ("AlignToLocation");
- }
- // Co-routine for animating the "snap" to the current location when the user releases
- IEnumerator AlignToLocation () {
- // Create and Set all of the current values in preparation of "lerping"
- bool aligning = true;
- // Create a "start time" to control the lerp
- float startTime = Time.time;
- // Create current location (normalized)
- float currentLocation = (obj.transform.rotation/360);
- // Find closest final location
- float closestLocation = increment * (Mathf.Round((obj.transform.rotation/360)/increment));
- // While aligning: Lerp the GameObject location to the next closest chapter
- while (aligning) {
- (obj.transform.rotation/360) = Mathf.Lerp (currentLocation, closestLocation, (Time.time - startTime) * alignmentSpeed);
- if (Mathf.Abs (closestLocation - (obj.transform.rotation/360)) < threshold) {
- (obj.transform.rotation/360) = closestLocation;
- aligning = false;
- }
- yield return null;
- }
- // Enable code here for triggering "something" AFTER the knob has settled
- DoSomething (closestLocation);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement