Skip to content

Instantly share code, notes, and snippets.

@todorok1
Last active May 4, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/00cf6935dd68021888be0b0abaa6f1f7 to your computer and use it in GitHub Desktop.
Save todorok1/00cf6935dd68021888be0b0abaa6f1f7 to your computer and use it in GitHub Desktop.
Unityチュートリアル・ゴルフゲームの進行を制御するスクリプト
IEnumerator GoalAnimation(float time){
// ゴール時のアニメーション処理
RectTransform rt = goalTextObject.GetComponent<RectTransform>();
// ゴールテキストオブジェクトの初期位置と移動先位置
Vector3 initTextPos = rt.localPosition;
Vector3 targetPos = Vector3.zero;
// 引数で渡された秒数を足した時間(現在時刻に足す)
float finishTime = Time.time + time;
while (true){
// 処理完了の時刻に達したかの確認
float diff = finishTime - Time.time;
if (diff <= 0){
break;
}
// Lerpを計算するために時間進行度を計算
float rate = 1 - Mathf.Clamp01(diff / time);
// 初期位置から移動先位置までの直線上で、割合に応じた位置をセット
rt.localPosition = Vector3.Lerp(initTextPos, targetPos, rate);
// 1フレーム待機
yield return null;
}
// 移動先位置をセット
rt.localPosition = targetPos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment