簡體   English   中英

使用腳本重新定位后,精靈對象從游戲選項卡中消失

[英]Sprite object disappears from the game tab after repositioning with script

我是 Unity 的新手,所以這可能是一個業余錯誤。 我正在嘗試創建一個簡單的游戲,該游戲在接收到觸摸輸入時將一個簡單的精靈圓圈移動到隨機位置。 該程序在場景選項卡中運行良好,但在接收觸摸輸入后,圓圈在游戲選項卡和連接的遠程設備中完全消失。

這是我連接到我的精靈圓 Object 的腳本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Main : MonoBehaviour
{
    private Touch theTouch;
    private Camera cam;

    // Start is called before the first frame update
    void Start()
    {
        cam = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)
        {
            theTouch = Input.GetTouch(0);
            if (theTouch.phase == TouchPhase.Ended)
            {
                MoveToRandomePosition();
            }
        }
    }

    // Random Movement
    void MoveToRandomePosition()
    {
        var position = cam.ScreenToWorldPoint(new Vector2(Random.Range(0, Screen.width), Random.Range(0, Screen.height)));
        transform.position = position;
    }
}

您可能想檢查相機和圓圈的 Z 坐標。 如果你在相機后面生成它,它就不會渲染。 如果您的凸輪位置為默認值 -10,您可以執行類似 position.z = 0 的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM