From the course: AR Development Techniques 02: Lighting and Physics

Tap on screen to place on plane: Part 2

From the course: AR Development Techniques 02: Lighting and Physics

Start my 1-month free trial

Tap on screen to place on plane: Part 2

- [Instructor] Now that we have detected the user touch, we'll project a raycast. For raycasting, we have to first add the AR Raycast Manager Script to the AR Session Origin object. So let's do that. Click on the AR Session Origin object, Add Component and we need to add AR Raycast Manager Script. The AR Raycast Manager Script has the raycast method, which is used to project the raycast. So we need to call that method. First, we'll create a variable of type ARSessionOrigin, because the AR Raycast Manager Script is attached to the AR Session Origin object. So I'll type in public ARSessionOrigin and to create ARSessionOrigin object, we need to import the AR foundation package using UnityEngine_XR_ARFoundation. We'll name this variable ar_session_origin. Save the script. Click on the ARObjectPlacement and we have this Ar_session_origin variable, which is empty right now. So drag this right here. Now that we have the reference to the ar_session_origin game object inside the script, you can get the AR Raycast Manager Script and its raycast method. So to call the raycast function, we will write ar_session_origin.GetComponent. We will get the AR Raycast Manager Script, which has the raycast function. ARRaycastManager, and we are going to call the raycast function. Now, this raycast function has three parameters. The first one is the screen point. It is the point at which the user touched the screen. So we can get this by Input.MousePosition, which is basically the touch position. The second parameter is the HitResults and it stores the info about the raycast that hit the detected surface. Now, this parameter is a list, so it means that there may be more than one raycast originating from the device but why would that happen? Well, that could happen when we do multi touch on the screen. That is tapping the screen with multiple fingers at once. But we just need the result for one finger tap. We will deal with that later. First, let's create a list variable where the raycast hits will be stored. So it will be public, it will be a List of RaycastHits and I'll name this raycastHits ARRaycastHit and we'll add this as the second argument. Now, the third parameter, which is trackable type, this will be UnityEngine.XR.ARSubsystems.TrackableType and we need PlaneWithPolygons. Now we have projected the raycast in the environment but we don't know of that raycast will hit a detected plane or not. We can only instant share an object when the raycast hits the detected plane. So to ensure that, we need an if condition. This line of code gives a Boolean result. True if the raycast hits the detected plane, false when the raycast doesn't hit the detected plane. So let's create a variable for this, a Boolean variable. And we'll write an if condition. If the collision happens, we will continue and we will instant share the object. So in the next video, we will see how to instant share a virtual cube at the point where a raycast meets the detected plane.

Contents