简体   繁体   English

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

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

I'm new to Unity so this can be an amateur mistake.我是 Unity 的新手,所以这可能是一个业余错误。 I'm trying to create a simple game that moves a simple sprite circle to a random position upon receiving touch input.我正在尝试创建一个简单的游戏,该游戏在接收到触摸输入时将一个简单的精灵圆圈移动到随机位置。 the program works well in Scene tab, but the circle fully disappears in game tab and the connected remote device after receiving touch input.该程序在场景选项卡中运行良好,但在接收触摸输入后,圆圈在游戏选项卡和连接的远程设备中完全消失。

Here's the script I have connected to my sprite circle Object :这是我连接到我的精灵圆 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;
    }
}

You may wanna check the Z coord of your camera and circle.您可能想检查相机和圆圈的 Z 坐标。 If you're spawning it behind the camera, it wont render.如果你在相机后面生成它,它就不会渲染。 You could do something like position.z = 0 if your cam pos is at the default -10.如果您的凸轮位置为默认值 -10,您可以执行类似 position.z = 0 的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM