Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created November 21, 2020 12:30
Show Gist options
  • Save todorok1/28ece5cddf25d8047b27c2d9cf3fe2d5 to your computer and use it in GitHub Desktop.
Save todorok1/28ece5cddf25d8047b27c2d9cf3fe2d5 to your computer and use it in GitHub Desktop.
Vector3のLerpメソッドを使ってみる
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