如何将玩家移动到2D Unity游戏中的特定点

时间:2014-11-15 22:38:04

标签: c# unity3d

我的游戏中有类似的东西:

Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{

}

查找播放器是否在窗口中。 我希望它将玩家移动到特定的坐标。 那么什么会将玩家传送到那些坐标?

1 个答案:

答案 0 :(得分:1)

尝试以下代码

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));
}