icywind / RemoteAssistantAR

Realtime remote chat and assistant APP with AR support
MIT License
28 stars 15 forks source link

Instantiating Prefabs with ARFoundation Remotely using RayCasting #5

Open Hisaack opened 4 years ago

Hisaack commented 4 years ago

Hi, I have tried to instantiate prefabs remotely by following the video you recommended me to watch. This the entire class for instantiating AR prefabs using AR RayCasting is as shown below. I have tried it remotely from Audience Scene but it fails. Any Idea please?

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

[RequireComponent(typeof(ARRaycastManager))]
public class PlacementController : MonoBehaviour
{
    [SerializeField]
    private GameObject placedPrefab;

    private Vector2 touchPosition = default;

    private ARRaycastManager arRaycastManager;

    private static List<ARRaycastHit> hits = new List<ARRaycastHit>();

    void Awake() => arRaycastManager = GetComponent<ARRaycastManager>();

    void Update()
    {
        if(Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            touchPosition = touch.position;

            if(touch.phase == TouchPhase.Began)
            {
                if(arRaycastManager.Raycast(touchPosition, hits, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
                {
                    Pose hitPose = hits[0].pose;
                    Instantiate(placedPrefab, hitPose.position, Quaternion.identity);
                }
            }  
        }
    }
}
Hisaack commented 4 years ago

Hello, any response please?

icywind commented 4 years ago

Please stick on one thread for conversations if this is still about improvement / adding new code. I am closing the other older issues.

As for your question - again, sorry I am unable to copy your code and test it. But let me give you some advices.

It seems this code needs to work with drawing from CastAR client side. Were you able to print out where those touch points are? Actually, remember there is Quad in front of the viewing camera. Your Raycast may have landed on the quad instead of the actual 3D world objects. If not, there is view different among the three cameras used in the scene. You may need to consider converting the screen touch points to raycast correctly on the AR Camera, and then convert to the RenderCamera's view port space for actual location. Yes, it is very complicated.
I suggest you first slow it down and make a smaller project to understand how the Raycast system works. And then refer to the blog I wrote to understand the architecture and structure more to see how this can fit into the project.

Hisaack commented 4 years ago

Honestly, I have tried with smaller projects. But the major problem is the differences between the cameras. The Raycast isn't landing on AR Space.

When you have free time, you can try and look by yourself.

Thanks.

FerPimenta commented 4 years ago

hi icywind.... did you find a solution for hardcoded z??? I lost many days trying a solution with raycast without sucess. I use agora io for years and this was the diferencial for my future projects. Thanks for the help and send a hi to Brian....

In other side, if hissack found a solution, please help me too. Thanks

francofreche commented 2 years ago

Hello, has anyone been able to solve this issue?