使用 ARCORE 进行适当的 Raycast
Proper Raycast with ARCORE
我尝试在我的应用程序中添加一些简单的光线投射效果。在编辑器中完美运行,但一旦我尝试将它与 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 设备对象中的第一人称相机上。我必须使用 TrackableHit 而不是 RaycastHit 吗?有人有合适的例子或可以帮助我更正我的代码吗?
终于成功了,我不得不在 public 中传递 ARController 并将其拖放到 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;
}
}
}
}
我尝试在我的应用程序中添加一些简单的光线投射效果。在编辑器中完美运行,但一旦我尝试将它与 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 设备对象中的第一人称相机上。我必须使用 TrackableHit 而不是 RaycastHit 吗?有人有合适的例子或可以帮助我更正我的代码吗?
终于成功了,我不得不在 public 中传递 ARController 并将其拖放到 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;
}
}
}
}