ブログ

オブジェクトを往復移動させる方法(Translate )

 


(スクリプト1)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour {

    private int count;
    private int a = 1;

	void Update () {
        count += 1;
        transform.Translate(a * Vector3.up * Time.deltaTime);

        if (count == 200)
        {
            count = 0;
            // 反転テクニック・・・>-1を掛ける
            //a = a * -1;
            a *= -1;
        }
    }
}

 


(スクリプト2)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{
    private int count;
    private int a = 1;

    void Update()
    {
        count += 1;
        transform.Translate(a * Vector3.up * Time.deltaTime);

        if (count % 200 == 0)
        {
            a *= -1;
        }
    }
}