もくじ
Unity ARで物体を投げる!
UnityのARでスマホをタップすることで、
物体を投げる動作をします。
以下のコードを実行することで
スマホをタップすることで、
Cubeをランダムな色でなげることができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
namespace GoogleARCore.Examples.HelloAR { using System.Collections.Generic; using GoogleARCore; using GoogleARCore.Examples.Common; using UnityEngine; using UnityEngine.EventSystems; #if UNITY_EDITOR using Input = InstantPreviewInput; #endif public class HelloARController : MonoBehaviour { public Camera FirstPersonCamera; public GameObject GameObjectVerticalPlanePrefab; public GameObject GameObjectHorizontalPlanePrefab; public GameObject GameObjectPointPrefab; private const float k_PrefabRotation = 180.0f; private bool m_IsQuitting = false; public void Awake() { Application.targetFrameRate = 60; } public void Update() { _UpdateApplicationLifecycle(); Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } //CreatePrimitiveで動的にGameObjectであるCubeを生成する。 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); //Cubeに適用するランダムな色を生成する。 Material material = new Material(Shader.Find("Diffuse")) { color = new Color(Random.value, Random.value, Random.value) }; //ランダムに変化する色をCubeに適用する。 cube.GetComponent<Renderer>().material = material; //Android端末をタップして、ランダムな色のCubeを認識した平面上に投げ出すように追加する。 //追加される、Cubeの大きさも0.2fとして指定する。 cube.transform.position = FirstPersonCamera.transform.TransformPoint(0, 0, 0.5f); cube.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f); //CubeにはRigidbodyを持たせて重力をあたえる。 //RigidBodyで重力を与えないとCubeが宙に浮くため。 cube.AddComponent<Rigidbody>(); cube.GetComponent<Rigidbody>().AddForce(FirstPersonCamera.transform.TransformDirection(0, 1f, 2f), ForceMode.Impulse); if (EventSystem.current.IsPointerOverGameObject(touch.fingerId)) { return; } } private void _UpdateApplicationLifecycle() { if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } if (Session.Status != SessionStatus.Tracking) { Screen.sleepTimeout = SleepTimeout.SystemSetting; } else { Screen.sleepTimeout = SleepTimeout.NeverSleep; } if (m_IsQuitting) { return; } if (Session.Status == SessionStatus.ErrorPermissionNotGranted) { _ShowAndroidToastMessage("Camera permission is needed to run this application."); m_IsQuitting = true; Invoke("_DoQuit", 0.5f); } else if (Session.Status.IsError()) { _ShowAndroidToastMessage( "ARCore encountered a problem connecting. Please start the app again."); m_IsQuitting = true; Invoke("_DoQuit", 0.5f); } } private void _DoQuit() { Application.Quit(); } private void _ShowAndroidToastMessage(string message) { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); if (unityActivity != null) { AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast"); unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => { AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>( "makeText", unityActivity, message, 0); toastObject.Call("show"); })); } } } } |
Unity ARで物体を投げる実行
以下の様にCubeをタップで投げることが
できました。
しかし、投げたCubeが地の果てへと
消えてしまっています。
次回は、Cubeが積みあがるように
したいと思います。