【Unity】ARKit床面認識して、ボールを落として弾ませる

環境メモ
⭐️Mac OS Mojave バージョン10.14
⭐️Xcode version 10.0(10A255)
⭐️Unity 2018.2.14f1
⭐️iPhone7 IOS11.4.1

実際に動かした動画はこちら↓↓
https://twitter.com/nonnonkapibara/status/1060524627447832576

<blockquote class="twitter-tweet" data-lang="ja"><p lang="ja" dir="ltr">床面認識して、ボールを落として<br>弾ませる<a href="https://twitter.com/hashtag/ARkit?src=hash&amp;ref_src=twsrc%5Etfw">#ARkit</a> <a href="https://twitter.com/hashtag/Unity3D?src=hash&amp;ref_src=twsrc%5Etfw">#Unity3D</a> <a href="https://t.co/R6Ysv1nvpz">pic.twitter.com/R6Ysv1nvpz</a></p>&mdash; non (@nonnonkapibara) <a href="https://twitter.com/nonnonkapibara/status/1060524627447832576?ref_src=twsrc%5Etfw">2018年11月8日</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>



1.まず、UnityARKitScenceを展開する
<img width="786" alt="000.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/34c9e736-d604-e8df-d6e6-264e0ae7c2dc.png">

2.非表示にする
<img width="1361" alt="001.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/1992c372-6e75-4703-ee30-cf0461cf4808.png">

3.ゲームオブジェクトを作る
<img width="574" alt="002.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/f1b360ad-99f1-d42b-14e8-f33e298d24eb.png">

4.上記で作ったゲームオブジェクトballObjectに名前を変える。
<img width="1364" alt="004.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/30055676-ba85-ff20-15f8-8585ad570c70.png">

5.HitCubeParentにする
<img width="497" alt="003.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/7706b52b-2c81-acc5-f383-37836c5b049b.png">

6.ボールを弾ませる透明なプレハブを設置する。
UnityARKitPluginのPrefabsからshadowPlanePrefab.prefabを配置する
<img width="960" alt="005.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/f3832254-eb3e-d193-8fd9-c3b84587dbc2.png">

7.shadowPlanePrefab.prefabの中からPlanにする
<img width="279" alt="007.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/65fe8592-13e1-6d4c-0826-8ac5025fc0e4.png">

<img width="1360" alt="008.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/68b9565e-bf1a-dc9d-643d-034d14aa6969.png">

<img width="446" alt="009.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/b7206139-1da4-4002-6330-180c2927c184.png">

<img width="362" alt="010.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/0c4128d9-1d80-7167-c3e1-3fc798a51c26.png">

<img width="665" alt="011.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/f6802b9f-818b-6c78-37a0-52b80a542c0e.png">

<img width="970" alt="012.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/3523d6cb-9688-f44f-7474-c1b14cbdfd2c.png">

<img width="979" alt="013.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/0492fd4b-3586-abe9-59a1-a62e261b358d.png">

<img width="1336" alt="014.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/d2cfe479-1cee-f081-2563-9f5382797cca.png">
<img width="650" alt="015.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/42e5d254-836c-f8f2-9b69-8d417cd641f5.png">

<img width="1437" alt="016.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/aac86dc0-bd6b-bb16-850d-0ef009be0455.png">

<img width="1436" alt="017.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/8c979711-ff66-5be3-2eb6-308592d12b8c.png">

<img width="373" alt="018.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/78c3c694-48bb-f3d9-9e92-20687803d905.png">

<img width="1439" alt="019.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/e286b62c-3446-8b22-886e-b2490ddb0ec5.png">

<img width="1438" alt="020.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/a97b0e55-398e-b0ea-7f36-b92bfb7fb5a2.png">

<img width="1437" alt="021.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/f68634de-6afd-9f18-bd6b-a425e252e915.png">
<img width="484" alt="022.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/1df4de76-87cf-ba1d-a576-e61749b48946.png">
<img width="310" alt="023.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/57ec0fdc-32cd-7910-d754-345e6b72ec04.png">
<img width="1439" alt="024.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/dc6b9d82-0ac8-895c-168c-de071442ffd7.png">
<img width="505" alt="025.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/d8b6dabf-e2ca-7f9d-459b-32b2b37d9476.png">
<img width="523" alt="026.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/86c85619-a029-52b0-27de-1e57d75a637b.png">
<img width="721" alt="027.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/94e7e134-0309-e80a-c32c-7f38e9b4aeed.png">
<img width="1136" alt="028.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/5d581512-9e60-7e1f-014d-a5eb827bd6f4.png">

「Bounciness」が０の場合、ボールは跳ねない。跳ねるには、１以上にする。
詳しいことは
こちらを参照
https://docs.unity3d.com/ja/current/Manual/class-PhysicMaterial.html
<img width="902" alt="スクリーンショット 2018-11-08 22.54.40.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/e9149dda-96f1-0beb-4fca-9445dc9d651b.png">


<img width="418" alt="029_1.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/1002a450-4073-8a73-ff0c-6f868e1bdc2f.png">

ボールを大きく弾ませるには、「Bounce Combine」を「Maximum」にする。
<img width="378" alt="029_2.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/642a4ce5-dc7e-0d73-64c8-e14d4bb9e82e.png">

<img width="1425" alt="030.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/1094149b-2185-e843-2931-c283726f9d63.png">
<img width="1432" alt="031.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/f6e248a1-52f3-5828-967f-cbd5c9b2dc51.png">
<img width="1438" alt="032.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/63eec223-760b-5fef-aa48-0c75d3cd0864.png">
<img width="1432" alt="033.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/e78c967f-61d2-682b-692c-f31693911c0e.png">
<img width="398" alt="034.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/a40fbc79-abf7-88db-ceb7-9ebdbedf6213.png">
<img width="652" alt="035.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/f896add0-ab46-b4e7-5923-29b00ab7efff.png">
<img width="1439" alt="036.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/32b97280-6512-08fc-1f56-76c1198114b3.png">
<img width="1439" alt="037.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/896dc090-60ec-9416-ab81-755f9f6da29c.png">
<img width="1314" alt="038.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/4be3674b-b0e0-e247-15e0-ac6c639e8757.png">
<img width="1325" alt="039.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/e0ad4060-2c99-d670-5e04-015b9fac5ef0.png">
<img width="1314" alt="040.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/07343439-b473-cc8d-e70b-036be6251ec8.png">
<img width="1320" alt="041.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/697bd0b0-e7c4-e071-17e9-f3c4ef4e9625.png">
<img width="1316" alt="042.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/c47a6b27-e4e0-f141-a6c0-cfbbb5d0514c.png">
<img width="1314" alt="043.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/82e80bad-3c7b-2ba2-87fc-110631c9035e.png">
<img width="1312" alt="044.png" src="https://qiita-image-store.s3.amazonaws.com/0/93155/b8ace60d-7749-6001-21b3-41848b620977.png">

```cs:BallScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallScript : MonoBehaviour
{
    public GameObject ball01;
    public GameObject ball02;
    public GameObject ball03;
    public GameObject ball04;
    public GameObject ball05;
    public GameObject ball06;
    public GameObject ball07;

    private float posistion01;
    private float posistion02;
    private float posistion03;
    private float posistion04;
    private float posistion05;
    private float posistion06;
    private float posistion07;

    // Use this for initialization
    void Start()
    {
        // Y軸を退避
        posistion01 = ball01.transform.position.y;
        posistion02 = ball02.transform.position.y;
        posistion03 = ball03.transform.position.y;
        posistion04 = ball04.transform.position.y;
        posistion05 = ball05.transform.position.y;
        posistion06 = ball06.transform.position.y;
        posistion07 = ball07.transform.position.y;

        // 重力無効化
        ball01.GetComponent<Rigidbody>().useGravity = false;
        ball02.GetComponent<Rigidbody>().useGravity = false;
        ball03.GetComponent<Rigidbody>().useGravity = false;
        ball04.GetComponent<Rigidbody>().useGravity = false;
        ball05.GetComponent<Rigidbody>().useGravity = false;
        ball06.GetComponent<Rigidbody>().useGravity = false;
        ball07.GetComponent<Rigidbody>().useGravity = false;
    }

    /*
     * 「Falling」ボタン押下
     */
    public void onTapFalling()
    {
        if (IsStop())
        {
            // ボールに重力をセットする
            ball01.GetComponent<Rigidbody>().useGravity = true;
            ball02.GetComponent<Rigidbody>().useGravity = true;
            ball03.GetComponent<Rigidbody>().useGravity = true;
            ball04.GetComponent<Rigidbody>().useGravity = true;
            ball05.GetComponent<Rigidbody>().useGravity = true;
            ball06.GetComponent<Rigidbody>().useGravity = true;
            ball07.GetComponent<Rigidbody>().useGravity = true;

            ball01.transform.position = new Vector3(ball01.transform.position.x, posistion01, ball01.transform.position.z);
            ball02.transform.position = new Vector3(ball02.transform.position.x, posistion02, ball02.transform.position.z);
            ball03.transform.position = new Vector3(ball03.transform.position.x, posistion03, ball03.transform.position.z);
            ball04.transform.position = new Vector3(ball04.transform.position.x, posistion04, ball04.transform.position.z);
            ball05.transform.position = new Vector3(ball05.transform.position.x, posistion05, ball05.transform.position.z);
            ball06.transform.position = new Vector3(ball06.transform.position.x, posistion06, ball06.transform.position.z);
            ball07.transform.position = new Vector3(ball07.transform.position.x, posistion07, ball07.transform.position.z);
        }

    }

    /*
     * ボール停止確認
     * 停止している場合はtrueを返す。動いている場合はfalseを返す。
     */
    private bool IsStop()
    {
        return ball01.GetComponent<Rigidbody>().IsSleeping() &&
                     ball02.GetComponent<Rigidbody>().IsSleeping() &&
                     ball03.GetComponent<Rigidbody>().IsSleeping() &&
                     ball04.GetComponent<Rigidbody>().IsSleeping() &&
                     ball05.GetComponent<Rigidbody>().IsSleeping() &&
                     ball06.GetComponent<Rigidbody>().IsSleeping() &&
                     ball07.GetComponent<Rigidbody>().IsSleeping();
    }
}

```