如何将玩家移动到 2D Unity 游戏中的特定点
本文关键字:游戏 Unity 2D 玩家 移动 | 更新日期: 2023-09-27 18:30:34
我的游戏中有这样的东西:
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
}
查找播放器是否不在窗口中。我希望它将玩家移动到特定的坐标。那么什么会把玩家传送到这些坐标呢?
尝试以下代码
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
float targetX = 100; // replace it with your value
float targetY = 100; // replace it with your value
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(targetX, targetY, camera.nearClipPlane));
}