簡體   English   中英

如何在2D Unity游戲中將玩家移動到特定點

[英]How to move the player to a specific point in a 2D Unity Game

我的游戲中有以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM