ブログ

上下する車体用エレベータの作成

(スクリプト)

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

public class Elevator : MonoBehaviour
{
    private Vector3 pos;
    private bool isTop = false;

    void Update()
    {
        pos = transform.position;

        if(!isTop)
        {
            transform.Translate(0, 0.01f, 0);
            if (pos.y > 20)
            {
                pos.y = 20;
                transform.position = pos;
                isTop = true;
            }
        }
        else if(isTop)
        {
            transform.Translate(0, -0.01f, 0);
            if (pos.y < -0.05f)
            {
                pos.y = -0.05f;
                transform.position = pos;
                isTop = false;
            }
        }
    }
}