ブログ

自動ドア(扉に近づくと、扉が開く)

(仕組み)


(扉と動かすスクリプト)

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

public class DoorMove : MonoBehaviour
{
    private Vector3 pos;

    void Update()
    {
        pos = transform.position;

        transform.Translate(0, 0.01f, 0);

        if(pos.y > 6f)
        {
            Destroy(gameObject);
        }
    }
}

(DoorMoveを起動させるスクリプト)

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

public class DoorOpen : MonoBehaviour
{
    public GameObject door;
    public AudioClip sound;

    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            door.GetComponent<DoorMove>().enabled = true;
            AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
            this.gameObject.SetActive(false);
        }
    }
}