如何在Unity AR基础中仅产生一个对象

发布于 2025-01-23 23:50:21 字数 1419 浏览 2 评论 0原文

我是新来的团结,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2025-01-30 23:50:21

如果您只想出现一次对象,则可以将布尔值添加到脚本中。

private bool objectSpawned = false;

然后在产生对象时将Bool设置为True。

        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    {
       
        Instantiate(ObjectToSpawn, hits[0].pose.position, ObjectToSpawn.transform.rotation);
        objectSpawned = true;
    }

现在,您将检查插入到update()函数中。这样,如果您已经产生了一个对象,您将无法再产生。

void Update()
{
   if(!objectSpawned)
   {
    ShowMarker();
   }
}

If you want to spawn your object only once, you can add a boolean check into your script.

private bool objectSpawned = false;

Then set your bool to true when you spawn the object.

        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    {
       
        Instantiate(ObjectToSpawn, hits[0].pose.position, ObjectToSpawn.transform.rotation);
        objectSpawned = true;
    }

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.

void Update()
{
   if(!objectSpawned)
   {
    ShowMarker();
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文