2015-10-06 39 views
0

當玩家離開屏幕時,我希望他們能夠傳送到屏幕的另一端,但這樣做效果不佳。我有兩個傳送物品附加了下面的腳本,但是當玩家進入對撞機時,它開始在兩者之間來回傳送(兩個對撞機都有下面的腳本和一個2D對話框作爲觸發器)。玩家傳送來回

using UnityEngine; 
using System.Collections; 

public class WallTransport : MonoBehaviour { 

    public Collider2D destination; 
    public LayerMask layer; 

    void OnTriggerEnter2D(Collider2D other) { 
     if(((1 << other.gameObject.layer) & layer) != 0) { 
      Vector2 destPos = destination.transform.position; 
      other.transform.position = new Vector2(destPos.x, other.transform.position.y); 
     } 
    } 
} 
+0

您可能要稍微改變'position'這樣玩家不會與目標碰撞傳送點它瞬移後。 –

回答

0
using UnityEngine; 
using System.Collections; 

public class WallTransport : MonoBehaviour { 

    public Collider2D destination; 
    public LayerMask layer; 

    void OnTriggerEnter2D(Collider2D other) { 
     if(((1 << other.gameObject.layer) & layer) != 0) { 
      Vector2 destPos = destination.transform.position; 
      other.transform.position = new Vector2(destPos.x + 1, other.transform.position.y); 
      //          ^^^^^^^^^^^^^ 
     } 
    } 
} 

例如,下面的圖像中,傳送點B位於在(10,0)和傳送器A位於(0,0)。假設他們從左到右進入傳送器B,而不是在他們傳送後在(0,0)處吐出玩家,則將他們置於(1,0),因此他們的勢頭將繼續朝着這個方向。

這只是一個簡單的例子,並不意味着你的情況是這樣的基本。

simple teleporter example

0

添加一個bool稱爲像「has_teleported」默認情況下它設置爲false ...... 切換這一點(真正的),當你進入一個瞬移對撞機,將其關閉(假)當您退出一個傳送對撞機。

只有關閉傳送功能時才能使用傳送功能。

Dub stylee的答案會起作用並且更加簡單,但是,這種方式更具動態性(因爲可以通過事件等來啓用和禁用遠程端口),並且在需要更改尺寸時可以擴展。

選擇最適合您的特定項目的方式,總是會有折衷的。

希望這有助於:)