我尝试在我的应用中添加一些简单的光线投射效果。在编辑器中完美地工作但是当我尝试在ARCORE中使用它时,我无法进行正确的光线投射...
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;
}
}
}
}
我将此代码放在ARCORe Device对象中的第一人称相机上。我是否必须使用TrackableHit而不是RaycastHit?有人有正确的例子还是可以帮我纠正我的代码?
答案 0 :(得分:0)
最后工作,我不得不公开传递ARController并拖拽和放大把它放在检查员手中!
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;
}
}
}
}