Search code examples
c#unity-game-enginearcore

Proper Raycast with ARCORE


I try to add some simple raycasting effect in my app. Work perfectly in Editor but as soon as I try to use it with ARCORE I'm unnable to make a proper raycast...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using GoogleARCore;


public class RaycastObject : MonoBehaviour
{
    public enum HoverState { HOVER, NONE };
    public HoverState hover_state = HoverState.NONE;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit)) //If collision
        {
            if (hover_state == HoverState.NONE && hit.transform.tag == "TestCollider") //if gamertag
            {
                Destroy(hit.transform.gameObject);
                hover_state = HoverState.HOVER;
            }
            else if(hover_state == HoverState.HOVER && hit.transform.tag != "TestCollider")
            {
                hover_state = HoverState.NONE;
            }
        }
        else
        {
            if (hover_state == HoverState.HOVER) //if gamertag
            {
                hover_state = HoverState.NONE;
            }
        }
    }
}

I put this code on my First Person Camera in ARCORe Device object. Does I have to use TrackableHit instead of RaycastHit ? Does someone have a proper example or can help me to correct my code ?


Solution

  • Finally work, I had to pass the ARController in public and drag & drop it in the Inspector !

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using GoogleARCore.HelloAR;
    
    public class RaycastCatchObject : MonoBehaviour
    {
        private GameObject childObject;
        public ExampleARController helloAr;
        public enum HoverState { HOVER, NONE };
        public HoverState hover_state = HoverState.NONE;
    
        private void Start()
        {
            helloAr._ShowAndroidToastMessage("Awake");
        }
    
        //Update is called once per frame
        public void FixedUpdate()
        {
            Vector3 fwd = transform.TransformDirection(Vector3.forward);
    
            RaycastHit hit;
    
            if (Physics.Raycast(transform.position, fwd, out hit)) //if hit a 3D Object
            {
                if (hover_state == HoverState.NONE && hit.transform.tag == "gameObjectCollider") //if object hit get a tag gameObjectCollider
                {
                    helloAr._ShowAndroidToastMessage("In");
                    hover_state = HoverState.HOVER;
                }
                else if (hover_state == HoverState.HOVER && hit.transform.tag != "gameObjectCollider")
                {
                    helloAr._ShowAndroidToastMessage("Out");
                    hover_state = HoverState.NONE;
                }
            }
            else
            {
                if (hover_state == HoverState.HOVER)
                {
                    helloAr._ShowAndroidToastMessage("Out");
                    hover_state = HoverState.NONE;
                }
            }
        }
    }