Android 当用户触摸游戏对象时,如何阻止ARCore模型放置?
免责声明:我是Unity3D和ARCore的新手,请耐心听我说 我正在使用Unity3D中的ARCore创建一个场景,用户可以在屏幕上的Android 当用户触摸游戏对象时,如何阻止ARCore模型放置?,android,raycasting,arcore,Android,Raycasting,Arcore,免责声明:我是Unity3D和ARCore的新手,请耐心听我说 我正在使用Unity3D中的ARCore创建一个场景,用户可以在屏幕上的滚动视图中选择模型,并使用Google的ARCore框架放置它们 到目前为止,我的工作;用户触摸滚动视图中的模型(在面板的屏幕上显示为当前选择的模型,因为我计划使用滚动视图切换可见性以获得更多屏幕视图空间) 问题是,当用户选择模型时,ARCore会将模型放置在检测到的平面上,滚动视图和所选模型面板对象所在的后面(即使您第一次触按开始滚动滚动视图)。请参见下面的内
滚动视图中选择模型,并使用Google的ARCore框架放置它们
到目前为止,我的工作;用户触摸滚动视图
中的模型(在面板
的屏幕上显示为当前选择的模型,因为我计划使用滚动视图
切换可见性以获得更多屏幕视图空间)
问题是,当用户选择模型时,ARCore会将模型放置在检测到的平面上,滚动视图
和所选模型面板
对象所在的后面(即使您第一次触按开始滚动滚动视图
)。请参见下面的内容以帮助可视化
如何使ARCore不将对象放置在滚动视图
和面板
后面?我尝试的是向我的控制器(实际上只是Google HelloARController)添加一个对象集合,我想阻止ARCore的Raycast
,并使用foreach
对它们进行迭代,以查看Raycast
是否命中集合中的GameObjects
Touch touch;
if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
{
return;
}
//my code; above is Google's
foreach (var item in BlockingObjects) { //BlockingObjects is a List<GameObject>
if (IsTouchInObject(FirstPersonCamera.ScreenPointToRay(touch.position), item)) {
return;
}
}
//end of my code; below is Google's
TrackableHit hit;
TrackableHitFlag raycastFilter = TrackableHitFlag.PlaneWithinBounds | TrackableHitFlag.PlaneWithinPolygon;
失败的是,rch.collider
总是null
(我知道我根本没有针对对象进行测试,一旦我能够让光线投射
与游戏对象
发生碰撞,我就会担心这一点)。我曾尝试使用物理/物理2D
和光线投射
/racashit2D
并将BoxCollider
/
组件附加到我想要检测命中的对象上,但我所做的一切都不起作用
(此解决方案取自Unity3D论坛上的某个内容,其中有人有类似问题,但与AR无关,他们自己的3D世界有2D覆盖。对不起,我找不到论坛帖子提供参考)
任何帮助都将不胜感激
编辑/注意:我现在注意到画布上有一个图形光线投射器
组件,其中包含我的滚动视图
和面板
。我尝试将阻塞对象设置为两个D
(同时将框碰撞器2D
添加到滚动视图和面板),并将阻塞遮罩设置为忽略光线投射(以及其他一些事情),但都没有效果。这些属性是否有一个值的组合可以做到这一点
本着今天的精神:
帮帮我,斯塔克…呃…克诺比。。。你是我唯一的希望。你有没有尝试过用以下材料包装光线投射:
if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId)) {...}
这是我关于StackOverflow的第一个答案,请耐心等待。我正在构建一个类似的应用程序,你可以从unity的UI面板中选择不同的模型,并将模型放置在现实世界中检测到的平面上。我所能想出的唯一解决问题的方法是执行以下操作:
在HelloARController.CS中,初始化bool“place_model”(或任何您想要的名称)并将其设置为false。现在,您需要滚动到在真实世界中增强模型的部分
if (Session.Raycast(m_firstPersonCamera.ScreenPointToRay(touch.position), raycastFilter, out hit))
{
// Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
// world evolves.
var anchor = Session.CreateAnchor(hit.Point, Quaternion.identity);
// Intanstiate an Andy Android object as a child of the anchor; it's transform will now benefit
// from the anchor's tracking.
var andyObject = Instantiate(m_andy, hit.Point, Quaternion.identity,
anchor.transform);
// Andy should look at the camera but still be flush with the plane.
andyObject.transform.LookAt(m_firstPersonCamera.transform);
andyObject.transform.rotation = Quaternion.Euler(0.0f,
andyObject.transform.rotation.eulerAngles.y, andyObject.transform.rotation.z);
andyObject.AddComponent<BoxCollider>();
// Use a plane attachment component to maintain Andy's y-offset from the plane
// (occurs after anchor updates).
andyObject.GetComponent<PlaneAttachment>().Attach(hit.Plane);
}
if(Session.Raycast(m_firstPersonCamera.screenpointRoay(touch.position)、raycastFilter、out-hit))
{
//创建一个锚,允许ARCore跟踪命中点,以了解物理过程
//世界在进化。
var anchor=Session.CreateAnchor(hit.Point,Quaternion.identity);
//将Andy Android对象安装为anchor的子对象;它的转换现在将受益
//从锚的追踪中。
var和yobject=实例化(m_-andy,hit.Point,Quaternion.identity,
锚定、转换);
//安迪应该看一下摄像机,但仍与飞机保持平齐。
andyoobject.transform.LookAt(m_firstPersonCamera.transform);
andyObject.transform.rotation=四元数.Euler(0.0f,
andyObject.transform.rotation.eulerAngles.y和andyObject.transform.rotation.z);
andyObject.AddComponent();
//使用平面附着构件保持Andy相对于平面的y偏移
//(在锚更新后发生)。
andyObject.GetComponent().Attach(hit.Plane);
}
将上述代码更改为类似以下内容:
if (place_model){ // changed portion
if (Session.Raycast(m_firstPersonCamera.ScreenPointToRay(touch.position), raycastFilter, out hit))
{
// Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
// world evolves.
var anchor = Session.CreateAnchor(hit.Point, Quaternion.identity);
// Intanstiate an Andy Android object as a child of the anchor; it's transform will now benefit
// from the anchor's tracking.
var andyObject = Instantiate(m_andy, hit.Point, Quaternion.identity,
anchor.transform);
// Andy should look at the camera but still be flush with the plane.
andyObject.transform.LookAt(m_firstPersonCamera.transform);
andyObject.transform.rotation = Quaternion.Euler(0.0f,
andyObject.transform.rotation.eulerAngles.y, andyObject.transform.rotation.z);
andyObject.AddComponent<BoxCollider>();
// Use a plane attachment component to maintain Andy's y-offset from the plane
// (occurs after anchor updates).
andyObject.GetComponent<PlaneAttachment>().Attach(hit.Plane);
place_model = false // changed portion
}
}
if(place_model){//更改部分
if(Session.Raycast(m_firstPersonCamera.screenpointRoay(touch.position)、raycastFilter、out-hit))
{
//创建一个锚,允许ARCore跟踪命中点,以了解物理过程
//世界在进化。
var anchor=Session.CreateAnchor(hit.Point,Quaternion.identity);
//将Andy Android对象安装为anchor的子对象;它的转换现在将受益
//从锚的追踪中。
var和yobject=实例化(m_-andy,hit.Point,Quaternion.identity,
锚定、转换);
//安迪应该看一下摄像机,但仍与飞机保持平齐。
andyoobject.transform.LookAt(m_firstPersonCamera.transform);
andyObject.transform.rotation=四元数.Euler(0.0f,
andyObject.transform.rotation.eulerAngles.y和andyObject.transform.rotation.z);
andyObject.AddComponent();
//使用平面附着构件保持Andy相对于t的y偏移
if (place_model){ // changed portion
if (Session.Raycast(m_firstPersonCamera.ScreenPointToRay(touch.position), raycastFilter, out hit))
{
// Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
// world evolves.
var anchor = Session.CreateAnchor(hit.Point, Quaternion.identity);
// Intanstiate an Andy Android object as a child of the anchor; it's transform will now benefit
// from the anchor's tracking.
var andyObject = Instantiate(m_andy, hit.Point, Quaternion.identity,
anchor.transform);
// Andy should look at the camera but still be flush with the plane.
andyObject.transform.LookAt(m_firstPersonCamera.transform);
andyObject.transform.rotation = Quaternion.Euler(0.0f,
andyObject.transform.rotation.eulerAngles.y, andyObject.transform.rotation.z);
andyObject.AddComponent<BoxCollider>();
// Use a plane attachment component to maintain Andy's y-offset from the plane
// (occurs after anchor updates).
andyObject.GetComponent<PlaneAttachment>().Attach(hit.Plane);
place_model = false // changed portion
}
}