0
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 3 years have passed since last update.

ARFoundation 平面検知、Rayを飛ばしてキャラを表示させる

Last updated at Posted at 2021-07-26

GameObjectで名前をCharacterに変更、キャラを子オブジェクトにしてPositionを0にする
51161C62-57C9-48E6-A45D-73C24D59ECA7_4_5005_c.jpeg

Characterはプレハブ化しておき、ヒエラルキーにあるCharacterは削除する
298EA4F2-D62D-4106-AF66-1AC059739487_4_5005_c.jpeg

ARSessionOriginにARRaycastManagerを追加する
7A9B890E-D0B8-4ADB-93E7-31ED11115BBD.jpeg

RayCast.csを作成する
44A39BC7-EC9B-461C-8DE6-60A2D55199A3_4_5005_c.jpeg

Raycast
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class Raycast : MonoBehaviour
{
    //Raycast を実行するためのクラス
    private ARRaycastManager m_RaycastManager;
    //Raycast の結果を格納する List
    private List<ARRaycastHit> hitResults = new List<ARRaycastHit>();
    //キャラクターの Prefab
    [SerializeField]
    private GameObject characterPrefab;
    void Start()
    {
        //GameObject にアタッチされている RaycastManager を取得
        m_RaycastManager = GetComponent<ARRaycastManager>();
    }
    void Update()
    {
        //画面がタッチされたら処理を行う
        if (Input.touchCount > 0)
        {
            //画面タッチの情報を取得する
            Touch touch = Input.GetTouch(0);
            //画面タッチの開始時のみ処理を行う
            if (touch.phase == TouchPhase.Began)
            {
                //タッチした方向に Ray を飛ばし、平面との衝突判定を行う
                if (m_RaycastManager.Raycast(
                touch.position,
                hitResults,
                TrackableType.PlaneWithinPolygon
                ))
                {
                    //最初に交差した平面から姿勢を取得して、GameObject を生成する
                    Pose hitPose = hitResults[0].pose;
                    GameObject character = Instantiate(
                    characterPrefab,
                    hitPose.position,
                    hitPose.rotation
                    );
                }
            }
        }
    }
}

AROriginにRayCast.csをアタッチし、Characterをアタッチする
5ECB3DA5-B237-4336-8B62-1BB0D916C72F.jpeg

ビルドする

0
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
0
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?