(リフトの作成)
- リフトの親オブジェクトは「Emptyオブジェクト」で作成する(Scale 1,1,1が重要なため)
- 実際に乗り込むリフトのBody部分は「Cube」で作成
(リフトを下降させるスクリプト)
- このスクリプトは親オブジェクトに追加する。
- チェックを外して、スクリプトをオフにする。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankLift : MonoBehaviour
{
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, new Vector3(0, -10, 0), 0.02f);
}
}
(「リフトスクリプトをオンにする」スクリプト)
- Cubeで作成したLift BodyにOnTriggerEnter用の「Sphere Collider」を追加する。
- Colliderの「大きさ」と「位置」を調整する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LiftSwitch : MonoBehaviour
{
public GameObject lift;
public GameObject tank;
private void OnTriggerEnter(Collider other)
{
// リフトのスイッチをオンにする
lift.GetComponent<TankLift>().enabled = true;
// 親子関係の設定
tank.transform.SetParent(lift.transform);
}
private void OnTriggerExit(Collider other)
{
// 親子関係の解除
tank.transform.parent = null;
}
}
(ポイント)
- リフトに乗り込むオブジェクトを、スクリプトで動的にリフト(親)の子供に設定する(これで下降中に跳ね上がるのを防止する)
- リフトから降りた段階で、親子関係を解除する。