C# 如何使用ARFoundation与对象交互?
因此,我们的想法是在增强现实中有一个平面和网格放置系统,能够在网格上放置和移动角色。我已经有了一个移动设备的例子,我有一个生成网格的脚本和一个允许我放置对象的脚本,它工作得很好,但是,我不知道如何使用上述所有内容,以及在AR中是否可行。例如,我想检测一个平面,然后实例化一个标高并在上面放置一些对象 以下是附加到GridManager并用于生成网格的脚本:C# 如何使用ARFoundation与对象交互?,c#,unity3d,augmented-reality,arkit,arcore,C#,Unity3d,Augmented Reality,Arkit,Arcore,因此,我们的想法是在增强现实中有一个平面和网格放置系统,能够在网格上放置和移动角色。我已经有了一个移动设备的例子,我有一个生成网格的脚本和一个允许我放置对象的脚本,它工作得很好,但是,我不知道如何使用上述所有内容,以及在AR中是否可行。例如,我想检测一个平面,然后实例化一个标高并在上面放置一些对象 以下是附加到GridManager并用于生成网格的脚本: [SerializeField] private float size = 0.05f; public Vector3 GetNearest
[SerializeField] private float size = 0.05f;
public Vector3 GetNearestPointOnGrid(Vector3 position)
{
position -= transform.position;
int xCount = Mathf.RoundToInt(position.x / size);
int yCount = Mathf.RoundToInt(position.y / size);
int zCount = Mathf.RoundToInt(position.z / size);
Vector3 result = new Vector3(
(float)xCount * size,
(float)yCount * size,
(float)zCount * size);
result += transform.position;
return result;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
for (float x = 0; x < 40; x += size)
{
for (float z = 0; z < 40; z += size)
{
var point = GetNearestPointOnGrid(new Vector3(x, 0f, z));
Gizmos.DrawSphere(point, 0.01f);
}
}
}
[SerializeField]专用浮点大小=0.05f;
公共向量3获取最接近点网格(向量3位置)
{
位置-=变换位置;
int xCount=数学圆整(位置x/尺寸);
int yCount=数学圆整(位置y/尺寸);
int zCount=数学圆整(位置z/尺寸);
Vector3结果=新Vector3(
(浮动)xCount*大小,
(浮动)Y计数*大小,
(浮动)Z计数*大小);
结果+=变换位置;
返回结果;
}
private void OnDrawGizmos()
{
Gizmos.color=color.yellow;
用于(浮动x=0;x<40;x+=大小)
{
用于(浮动z=0;z<40;z+=大小)
{
var point=GetNearestPointOnGrid(新向量3(x,0f,z));
Gizmos.DrawSphere(点,0.01f);
}
}
}
下面是一个连接到PlacerManager并用于在网格上放置对象的:
private Grid grid;
private void Awake()
{
grid = FindObjectOfType<Grid>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
PlaceCubeNear(hitInfo.point);
}
}
}
private void PlaceCubeNear(Vector3 clickPoint)
{
var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = finalPosition;
}
私有网格;
私人空间
{
grid=FindObjectOfType();
}
私有void更新()
{
if(Input.GetMouseButtonDown(0))
{
RaycastHitInfo;
Ray-Ray=Camera.main.screenpointoray(输入.鼠标位置);
if(物理.光线投射(光线,输出hitInfo))
{
PlaceCubeNear(hitInfo.point);
}
}
}
私有void PlaceCubeNear(矢量3点击点)
{
var finalPosition=grid.GetNearestPointOnGrid(点击点);
GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position=finalPosition;
}
您可以使用光线投射选项来识别不同的对象您可以使用光线投射选项来识别不同的对象光线投射和/或碰撞器是一种方法
AAR基金会中的示例场景有一个名为Posion Pr.E.Cs的脚本,它显示了如何检测用户触摸屏幕的时间。例如:
if (Input.touchCount == 1) {
if (m_RaycastManager.Raycast(Input.GetTouch(0).position, s_Hits, TrackableType.PlaneWithinPolygon))
{
// Raycast hits are sorted by distance, so the first one
// will be the closest hit.
var hitPose = s_Hits[0].pose;
if (spawnedObject == null)
{
spawnedObject = Instantiate(m_PlacedPrefab, hitPose.position, hitPose.rotation);
}
}
}
这允许您获得触摸屏的位置,然后从该位置向真实场景进行光线投射。在本例中,游戏对象在该位置实例化。对于您的情况,如果您的命中位置周围存在一个平面或一个平面,则可以实例化一个级别。光线投射和/或碰撞器是最佳选择
AAR基金会中的示例场景有一个名为Posion Pr.E.Cs的脚本,它显示了如何检测用户触摸屏幕的时间。例如:
if (Input.touchCount == 1) {
if (m_RaycastManager.Raycast(Input.GetTouch(0).position, s_Hits, TrackableType.PlaneWithinPolygon))
{
// Raycast hits are sorted by distance, so the first one
// will be the closest hit.
var hitPose = s_Hits[0].pose;
if (spawnedObject == null)
{
spawnedObject = Instantiate(m_PlacedPrefab, hitPose.position, hitPose.rotation);
}
}
}
这允许您获得触摸屏的位置,然后从该位置向真实场景进行光线投射。在本例中,游戏对象在该位置实例化。对于您的情况,如果命中平面或命中位置周围存在平面,则可以实例化一个级别