Created
February 1, 2016 09:38
-
-
Save gok11/3d1181cd9935d7ed1e37 to your computer and use it in GitHub Desktop.
AnimationCurveを使って値を変化させる ref: http://qiita.com/backlight1144/items/f3005bdc6324ba8b491b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class LinearMove : MonoBehaviour { | |
| [SerializeField] private Vector3 destPos; | |
| [SerializeField] private float tweenTime; | |
| private bool isMoving = false; | |
| // Use this for initialization | |
| void Start () { | |
| StartCoroutine(MoveCor()); | |
| } | |
| IEnumerator MoveCor() | |
| { | |
| if (isMoving) yield break; | |
| isMoving = true; | |
| var startTime = Time.time; | |
| var initPos = transform.position; | |
| while (Time.time - startTime < tweenTime) | |
| { | |
| float rate = (Time.time - startTime) / tweenTime; | |
| transform.position = Vector3.Lerp(initPos, destPos, rate); | |
| yield return new WaitForEndOfFrame(); | |
| } | |
| transform.position = destPos; | |
| isMoving = false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class TweenMove : MonoBehaviour { | |
| [SerializeField] private Vector3 destPos; | |
| [SerializeField] private float tweenTime; | |
| [SerializeField] private AnimationCurve tweenCurve = AnimationCurve.EaseInOut(0, 0, 1, 1); | |
| private bool isTweening = false; | |
| // Use this for initialization | |
| void Start () { | |
| StartCoroutine(MoveCor()); | |
| } | |
| IEnumerator MoveCor() | |
| { | |
| if (isTweening) yield break; | |
| isTweening = true; | |
| var startTime = Time.time; | |
| var initPos = transform.position; | |
| while (Time.time - startTime < tweenTime) | |
| { | |
| float rate = (Time.time - startTime) / tweenTime; | |
| var curvedRate = tweenCurve.Evaluate(rate); | |
| transform.position = Vector3.Lerp(initPos, destPos, curvedRate); | |
| yield return new WaitForEndOfFrame(); | |
| } | |
| transform.position = destPos; | |
| isTweening = false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment