示例#1
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (_canActivate)
        {
            if (collision.gameObject.CompareTag("Player")) //If the player entered the trigger
            {
                _connectedTeleporter.DisableActivation();

                //Spawn the "explosions"
                Instantiate(Resources.Load("Prefabs/ExplosionPrefab"), transform.position + new Vector3(0, 0, 0.3f), Quaternion.identity);
                Instantiate(Resources.Load("Prefabs/ExplosionPrefab"), _connectedTeleporter.transform.position + new Vector3(0, 0, 0.3f), Quaternion.identity);


                //"Teleport" the player
                Vector3 newPlayerPos = collision.gameObject.transform.position;
                newPlayerPos = new Vector3(_connectedTeleporter.transform.position.x, _connectedTeleporter.transform.position.y, newPlayerPos.z);
                collision.gameObject.transform.position = newPlayerPos;

                //Get the relevant data from the player
                PlayerObject playerObject = collision.gameObject.GetComponent <PlayerObject>();
                Transform[]  playerNodes  = playerObject.GetNodes();

                for (int i = 0; i < playerNodes.Length; i++) //Loop through the players' nodes and find the node that corresponds to the connected teleporter.
                {
                    if ((Vector2)playerNodes[i].position == (Vector2)_connectedTeleporter.transform.position)
                    {
                        playerObject.SetNextNode(i);
                        break;
                    }
                }
            }
        }
    }