[英]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.