LoginSignup
0
0

More than 3 years have passed since last update.

【Unity】往復処理

Last updated at Posted at 2020-08-22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// ずっと、往復する
public class Forever_MoveSin : MonoBehaviour
{
    public float speedX = 1; // スピードX:Inspectorで指定
    public float speedY = 0; // スピードY:Inspectorで指定
    public float speedZ = 0; // スピードZ:Inspectorで指定
    public float second = 1; // かかる秒数:Inspectorで指定

    float time = 0f;

    private void FixedUpdate()  // ずっと、往復する
    {
        time += Time.deltaTime;
        float s = Mathf.Sin(time * 3.14f / second); // 移動量を求める
        this.transform.Translate(speedX * s / 50, speedY * s / 50, speedZ * s / 50); 
    }
}

secondの値を変更すると、その値の秒数間でアタッチされたゲームオブジェクトが往復する。

下記のGifは
インスペクターから
second = 6
に値を変更した例

つまり、6秒間で往復を行う

ezgif.com-video-to-gif.gif

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