68 lines
		
	
	
	
		
			2.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
	
		
			2.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System.Collections;
 | 
						|
using System.Collections.Generic;
 | 
						|
using UnityEngine;
 | 
						|
 | 
						|
public class PortalController : MonoBehaviour{
 | 
						|
 | 
						|
    [System.Serializable] public enum Direction{Left, Right, Up, Down}
 | 
						|
    [SerializeField] Direction direction = default;
 | 
						|
 | 
						|
    [SerializeField] Transform exitPos = default;
 | 
						|
 | 
						|
 | 
						|
    bool teleporting;
 | 
						|
    PlayerController player;
 | 
						|
    Animator anim;
 | 
						|
 | 
						|
    void Awake(){
 | 
						|
        anim = GetComponentInParent<Animator>();
 | 
						|
        player = FindObjectOfType<PlayerController>();
 | 
						|
    }
 | 
						|
 | 
						|
    void OnEnable(){
 | 
						|
        anim.SetBool("Close", false);
 | 
						|
        teleporting = false;
 | 
						|
    }
 | 
						|
 | 
						|
    void OnTriggerStay(Collider col){
 | 
						|
        if (col.CompareTag("Player")){
 | 
						|
            switch (direction){
 | 
						|
                case Direction.Left:
 | 
						|
                    if (Input.GetAxisRaw("Horizontal") < 0 && !teleporting){
 | 
						|
                        player.Teleport(exitPos.position);
 | 
						|
                        StartCoroutine(ClosePortal());
 | 
						|
                        teleporting = true;
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case Direction.Right:
 | 
						|
                    if (Input.GetAxisRaw("Horizontal") > 0 && !teleporting){
 | 
						|
                        player.Teleport(exitPos.position);
 | 
						|
                        StartCoroutine(ClosePortal());
 | 
						|
                        teleporting = true;
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case Direction.Up:
 | 
						|
                    if (Input.GetAxisRaw("Vertical") > 0 && !teleporting){
 | 
						|
                        player.Teleport(exitPos.position);
 | 
						|
                        StartCoroutine(ClosePortal());
 | 
						|
                        teleporting = true;
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
                case Direction.Down:
 | 
						|
                    if (Input.GetAxisRaw("Vertical") < 0 && !teleporting){
 | 
						|
                        player.Teleport(exitPos.position);
 | 
						|
                        StartCoroutine(ClosePortal());
 | 
						|
                        teleporting = true;
 | 
						|
                    }
 | 
						|
                    break;
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    IEnumerator ClosePortal(){
 | 
						|
        yield return new WaitForSeconds(.25f);
 | 
						|
        anim.SetBool("Close", true);
 | 
						|
        yield return new WaitForSeconds(.5f);
 | 
						|
        transform.parent.gameObject.SetActive(false);
 | 
						|
    }
 | 
						|
}
 |