1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】ねずみちゃんAR(ねずみちゃんTapでカウントアップ)

Last updated at Posted at 2020-01-02

環境メモ
⭐️Mac OS Mojave バージョン10.14
⭐️Unity 2019.1.9f1
⭐️Xcode11.2
⭐️iPhone7(IOS 12.2)

UnityとARKitで、ねずみちゃんAR(ねずみちゃんTapでカウントアップ)を作る。

↓↓実際に動かした動画

準備

まず、ねずみちゃんを表示する「チーズ」モデルをBlenderで作成。
001.png

cheese.fbxで出力する。

AR表示する ねずみちゃんモデルをBlenderで作成。
002.png

mouse.fbxで出力する

ここからUnity

Unityを起動する。
Unity AR Kitを取り込む。

【Unity】Unity ARKit Pluginが無いのでUnity-Technologies-unity-arkit-plugin-b3de628acfac.zipを使う
https://qiita.com/nonkapibara/items/3800d4ff3c56912073df

UnityARKitPlugin - Examples - UnityARKitScene
を選ぶ
003.png

RandomCube
HitCubeParent
GeneratePlanes
ARKitControl
PointCloudExample
AR3DOFCameraManager
は不要なので削除する
004.png

005.png

準備で用意した、「チーズ」モデルと、ねずみちゃんモデルをUnityに取り込む
006.png

「チーズ」モデルをPrefabにして、タグを「cheese」 にする
007.png

AR表示するチーズを配置する
008.png

たくさんのチーズを1つにまとめる。
Create Emptyを作成し、その中に、たくさん作ったチーズを入れる
009.png

010.png

まとめたチーズを、Prefabにする
011.png

次に、ねずみちゃんをPrefabにする
012.png

タグを「mouse」にする。
ねずみちゃんに、Colliderを設定する
013.png

014.png 015.png 016.png

次に、チーズの床を作る。(ねずみちゃんをDownした時に、隠れるようにする)
3D ObjectーPlanを選択する
017.png

018.png

チーズの床を「occlusionPlaneMaterial.mat」にする

UnityARKitPlugin - Examples - Common - Materials
「occlusionPlaneMaterial.mat」を選択する
019.png

020.png

ねずみちゃんTapでカウントアップ

Tapの数値を画面に表示する
021.png

022.png

ポイント表示する為に、設定する
023.png

024.png

ねずみちゃんTapでパーティクルを表示する

025.png 026.png 027.png 028.png 029.png

ソースコード

Create Emptyで、Game Objectを作り、
「floorScript」と「mouseHitScript」を配置する
スクリーンショット 2020-01-03 0.04.23.png

floorScript.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;

public class floorScript : MonoBehaviour
{
    bool cheeseViewed = false; // チーズモデル描画
    public GameObject cheeaseObj; // チーズ
    public GameObject mouseObj; // ネズミ

    public void CreateMouse(GameObject mouse)
    {
        foreach(Transform trans in mouse.transform)
        {
            GameObject lastLayer = trans.gameObject;
            if (lastLayer.tag == "cheese") {
                Vector3 position = lastLayer.transform.position;
                Instantiate(mouseObj, position, mouseObj.transform.rotation);
            }
        }
    }


    void HitTest(ARPoint point)
    {
        List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent);

        if (hitResults.Count > 0)
        {
            GameObject cheese = Instantiate(cheeaseObj);
            cheese.transform.position = UnityARMatrixOps.GetPosition(hitResults[0].worldTransform);
            cheese.transform.rotation = UnityARMatrixOps.GetRotation(hitResults[0].worldTransform);

            // ネズミを生成する
            CreateMouse(cheese);

            this.cheeseViewed = true; // チーズモデル描画済み
        }
    }

    void Update()
    {
        if ((!cheeseViewed && Input.touchCount > 0) 
            && (Input.GetTouch(0).phase == TouchPhase.Began))
        {
            var touchPosition = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position);
            ARPoint arPoint = new ARPoint
            {
                x = touchPosition.x, y = touchPosition.y
            };

            HitTest(arPoint);
        }
        
    }
}

mouseHitScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class mouseHitScript : MonoBehaviour
{
    int hitCount = 0;

    void Update()
    {
        // ねずみちゃんTapする
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit = new RaycastHit();
            if (Physics.Raycast(ray, out hit))
            {
                pointMode(hit);
            }
        }
    }

    void pointMode(RaycastHit hit)
    {
        // ねずみちゃんをTapしたらポイントをカウントアップする
        if (hit.transform.gameObject.CompareTag("mouse"))
        {
            hit.transform.gameObject.GetComponent<mouseScript>().HitProcess();
            hitCount++;
            GameObject.Find("Point").GetComponent<Text>().text = "Point: " + hitCount.ToString();
        }
    }
}

ねずみちゃんPrefabに、「mouseScript」を配置し、パーティクルをDrag&Dropして配置する
スクリーンショット 2020-01-03 0.09.11.png

mouseHitScript.cs

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

public class mouseScript : MonoBehaviour
{
    Vector3 mouseViewPosition; // ねずみちゃん表示位置
    Vector3 mouseViewDownPosition; // ねずみちゃん下がった時の位置
    bool mouseUppedFlag = true; // ねずみちゃんUP
    float timeCount = 0;
    public GameObject effect;

    void Start()
    {
        Vector3 pos = transform.position;
        pos.y += 0.05f;
        this.mouseViewPosition = pos;

        this.mouseViewDownPosition = this.mouseViewPosition - new Vector3(0, 0.1f, 0);
        // ねずみちゃんDown
        DownUnder();
    }

    void UpAbove()
    {
        transform.position = this.mouseViewPosition;
        mouseUppedFlag = true; // ねずみちゃんUP
    }

    void DownUnder()
    {
        transform.position = this.mouseViewDownPosition;
        mouseUppedFlag = false; // ねずみちゃんDown
    }

    private void Update()
    {
        this.timeCount += Time.deltaTime;
        if (this.timeCount > 3.0f)
        {
            this.timeCount = 0;
            if (this.mouseUppedFlag) {
                DownUnder();
            } else {
                UpAbove();
            }

        }
    }

    public void HitProcess()
    {
        GameObject particleObj = Instantiate(effect, transform.position + new Vector3(0, 0.04f, 0), effect.transform.rotation);
        Destroy(particleObj, 1.0f);

        this.timeCount = 0;
        // ねずみちゃんを下に下げる
        DownUnder();
    }
}

完成!
スクリーンショット 2020-01-02 23.35.34.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?