Created
September 13, 2020 02:15
-
-
Save takashi1975/a9e0f2afb058b70f42e80fcd25c2efb6 to your computer and use it in GitHub Desktop.
Unity ゲームオブジェクトの往復移動
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; | |
/// <summary> | |
/// Lerpでゲームオブジェクトを移動させる | |
/// | |
/// [参考URL] | |
/// UnityでLerpを使って収縮運動や往復運動をさせる | |
/// https://getabakoclub.com/2020/01/30/lerp%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E5%8F%8E%E7%B8%AE%E9%81%8B%E5%8B%95%E3%82%84%E5%BE%80%E5%BE%A9%E9%81%8B%E5%8B%95%E3%82%92%E3%81%95%E3%81%9B%E3%82%8B/ | |
/// </summary> | |
public class RoundTrip : MonoBehaviour | |
{ | |
private Vector3 originPos; //最初の位置 | |
[SerializeField] private Vector3 offsetPos; //移動量 | |
//RoundTrip | |
private float MoveTimer = 0f; //往復判断用 | |
[SerializeField] private float RoundTripSpan = 2f; //往復期間[秒] | |
private void Start() { | |
this.originPos = this.transform.localPosition; | |
} | |
void Update() | |
{ | |
this.MoveTimer += Time.deltaTime; | |
if (this.MoveTimer < this.RoundTripSpan * 0.5f) { | |
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, this.originPos + this.offsetPos, 0.1f); | |
} | |
if ((this.MoveTimer >= this.RoundTripSpan * 0.5) && | |
(this.MoveTimer < this.RoundTripSpan)) { | |
this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, this.originPos, 0.1f); | |
} | |
if (this.MoveTimer >= this.RoundTripSpan) { | |
this.MoveTimer = 0f; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment