0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Unity】いったりきたりするオブジェクトをつくる

Last updated at Posted at 2020-01-22

定型文みたいなものなので、基本コピペでいいと思います

これをスクリプトで書くなら、Animationのほうがずっと楽です。
【Unity】ノンコーディングでオブジェクトを動かしたい【Animation】

using UnityEngine;
using System.Collections;


public class ittarikitari : MonoBehaviour
{
    [SerializeField]
    private Vector3 _startPosition;

    [SerializeField]
    private Vector3 _targetPosition;

    [SerializeField]
    private float _duration = 1f;

    private float _time = 0;
    //private int _dirFactor = 1;
    private bool _inverse = false;

    private void Update()
    {
        _time += Time.deltaTime;

        // 指定時間を過ぎたら向きを逆に
        if (_time >= _duration)
        {
            _time = 0;
            _inverse = !_inverse;
        }

        // 時間を媒介変数として計算(0 - 1)
        float t = _time / _duration;

        if (_inverse)
        {
            transform.position = Vector3.Lerp(_startPosition, _targetPosition, 1f - t);
        }
        else
        {
            transform.position = Vector3.Lerp(_startPosition, _targetPosition, t);
        }
    }
}

コンポーネントの見た目はこんな感じ
j.png

いったりきたりする
k.png
l.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?