Vector3のLerpメソッドを使ってみる
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class VectorTest : MonoBehaviour | |
{ | |
// 値を変化させる秒数です。 | |
float animTime = 2.0f; | |
// アニメーションの終了予定時刻です。 | |
float finishTime; | |
// アニメーションの初期位置です。 | |
Vector3 initPos; | |
// アニメーションの終了位置です。 | |
Vector3 targetPos; | |
void Start() | |
{ | |
// アニメーションを開始できるように情報をセットします。 | |
finishTime = Time.time + animTime; | |
initPos = gameObject.transform.position; | |
targetPos = new Vector3(initPos.x + 15.0f, initPos.y, initPos.z); | |
} | |
void Update() | |
{ | |
if (Time.time < finishTime) | |
{ | |
// アニメーションの残り時間を取得します。 | |
float restTime = finishTime - Time.time; | |
// アニメーションの経過時間を取得します。 | |
float elapsedTime = Mathf.Clamp01((finishTime - restTime) / animTime); | |
// 時間に応じた現在の位置を取得してセットします。 | |
Vector3 pos = Vector3.Lerp(initPos, targetPos, elapsedTime); | |
gameObject.transform.position = pos; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment