如何在Unity AR基础中仅产生一个对象
我是新来的团结,C#有人可以帮我吗?我正在创建一个儿童游戏,他们倾向于意外地在屏幕上敲击屏幕,而它们环顾四周,但随后它们再次催生了该物体,并且在第一个物体上。当我触摸屏幕对象是催生时,我只想。 sory我的英语。 谢谢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ProgrammManager : MonoBehaviour
{
[Header("Put your planeMarker here")]
[SerializeField] private GameObject PlaneMarkerPrefab;
private ARRaycastManager ARRaycastManagerScript;
private Vector2 TouchPosition;
public GameObject ObjectToSpawn;
void Start()
{
ARRaycastManagerScript = FindObjectOfType<ARRaycastManager>();
PlaneMarkerPrefab.SetActive(false);
}
void Update()
{
ShowMarker();
}
void ShowMarker()
{
List<ARRaycastHit> hits = new List<ARRaycastHit>();
ARRaycastManagerScript.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.Planes);
if (hits.Count > 0)
{
PlaneMarkerPrefab.transform.position = hits[0].pose.position;
PlaneMarkerPrefab.SetActive(true);
}
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Instantiate(ObjectToSpawn, hits[0].pose.position, ObjectToSpawn.transform.rotation);
}
}
}
I new for Unity and C# can someone help me? I'm creating a kids game and they tend to tap on the screen accidentally and as they look around but then they spawn the object again and its over the first one. I just want when i touch screen object was ones spawned. Sory for my English.
Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ProgrammManager : MonoBehaviour
{
[Header("Put your planeMarker here")]
[SerializeField] private GameObject PlaneMarkerPrefab;
private ARRaycastManager ARRaycastManagerScript;
private Vector2 TouchPosition;
public GameObject ObjectToSpawn;
void Start()
{
ARRaycastManagerScript = FindObjectOfType<ARRaycastManager>();
PlaneMarkerPrefab.SetActive(false);
}
void Update()
{
ShowMarker();
}
void ShowMarker()
{
List<ARRaycastHit> hits = new List<ARRaycastHit>();
ARRaycastManagerScript.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.Planes);
if (hits.Count > 0)
{
PlaneMarkerPrefab.transform.position = hits[0].pose.position;
PlaneMarkerPrefab.SetActive(true);
}
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Instantiate(ObjectToSpawn, hits[0].pose.position, ObjectToSpawn.transform.rotation);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想出现一次对象,则可以将布尔值添加到脚本中。
然后在产生对象时将Bool设置为True。
现在,您将检查插入到update()函数中。这样,如果您已经产生了一个对象,您将无法再产生。
If you want to spawn your object only once, you can add a boolean check into your script.
Then set your bool to true when you spawn the object.
Now, you insert your check into the Update() function. This way, if you already spawned an object, you won't be able to spawn any more.