Advertisement
drakon-firestone

Portal

Apr 19th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Portal : MonoBehaviour
  6. {
  7. private const string PLAYER_TAG = "Player";
  8.  
  9. [SerializeField] Transform otherPortal;
  10.  
  11. [SerializeField] private Camera myCamera;
  12.  
  13. public Camera GetMyCamera()
  14. {
  15. return myCamera;
  16. }
  17.  
  18. [SerializeField] private Transform myRenderPlane;
  19. [SerializeField] private Transform myCollider;
  20.  
  21. // public Transform GetMyCollider() => myCollider;
  22. public Transform GetMyCollider()
  23. {
  24. return myCollider;
  25. }
  26.  
  27. [SerializeField] Material material;
  28.  
  29. private GameObject player;
  30. private PortalCamera portalCamera;
  31. private PortalTeleport portalTeleport;
  32.  
  33. private void Awake()
  34. {
  35. portalCamera = myCamera.GetComponent<PortalCamera>();
  36. portalTeleport = myCollider.GetComponent<PortalTeleport>();
  37. player = GameObject.FindGameObjectWithTag(PLAYER_TAG);
  38.  
  39. //// jeżeli kamera jest jako 1 dziecko obiektu gracza
  40. //portalCamera.playerCamera = player.transform.GetChild(0);
  41.  
  42. //// wykorzystujemy główną kamera - oznaczoną tagiem "MainCamera"
  43. //portalCamera.playerCamera = Camera.main.transform;
  44.  
  45. portalCamera.playerCamera = player.GetComponentInChildren<Camera>().transform;
  46. portalCamera.otherPortal = otherPortal.transform;
  47. portalCamera.portal = transform;
  48.  
  49. portalTeleport.player = player.transform;
  50. portalTeleport.receiver = otherPortal.GetComponent<Portal>().GetMyCollider();
  51.  
  52. myRenderPlane.GetComponent<Renderer>().material = Instantiate(material);
  53. if (myCamera.targetTexture != null)
  54. {
  55. myCamera.targetTexture.Release();
  56. }
  57. myCamera.targetTexture = new RenderTexture(Screen.width, Screen.height, 24);
  58.  
  59. float myAngle = transform.localEulerAngles.y % 360;
  60. portalCamera.SetMyAngle(myAngle);
  61. }
  62.  
  63. private void Start()
  64. {
  65. myRenderPlane.GetComponent<Renderer>().material.mainTexture =
  66. otherPortal.GetComponent<Portal>().GetMyCamera().targetTexture;
  67. }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement