コライダのオブジェクトにアサインする
スクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class door : MonoBehaviour {
public Transform _RdoorRoot;//右ドアのルート入れる
public Transform _LdoorRoot;//左ドアのルート入れる
bool doorCheckBool = false;//ブーリアン
float stTime;//コライダにヒット開始時間
public Transform from;//元の角度
public Transform to;//90度の角度を入れたnullをアサイン
public Transform _to;//−90度の角度を入れたnullをアサイン
public float speed;//回転速度
void Update () {
if (doorCheckBool) {
_RdoorRoot.rotation = Quaternion.Slerp (from.rotation, to.rotation, (Time.time - stTime) * speed);
_LdoorRoot.rotation = Quaternion.Slerp (from.rotation, _to.rotation, (Time.time - stTime) * speed);
}
}
void OnTriggerEnter(Collider other) {//コライダ入ったら
doorCheckBool = true;//ブールをtrueにするとUpdateで作動する
stTime = Time.time;//現在の時間を記録しておく
GetComponent<BoxCollider>().enabled = false;//1回作動させたらコライダーをオフにして使用できなくする
}
}
配置方法
実行結果
