使用脚本重新定位后,Sprite 对象从游戏选项卡中消失

Sprite object disappears from the game tab after repositioning with script

我是 Unity 的新手,所以这可能是一个业余错误。 我正在尝试创建一个简单的游戏,在接收到触摸输入时将一个简单的精灵圆圈移动到一个随机位置。 该程序在场景选项卡中运行良好,但在接收到触摸输入后,圆圈在游戏选项卡和连接的远程设备中完全消失。

这是我连接到精灵圈对象的脚本:

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 坐标。如果您在相机后面生成它,它不会渲染。 如果你的 cam pos 是默认的 -10,你可以做类似 position.z = 0 的事情。