Advertisement
_EagleOwle_

CheckScreenEdges

Jun 24th, 2021
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CheckScreenEdges : MonoBehaviour
  6. {
  7.     public enum ScreenEdge
  8.     {
  9.         None,
  10.         Up,
  11.         Down,
  12.         Right,
  13.         Left,
  14.         UpRight,
  15.         UpLeft,
  16.         DownRight,
  17.         DownLeft
  18.     }
  19.  
  20.     [SerializeField] private CameraFreeFly cameraFreeFly;
  21.     private ScreenEdge edge;
  22.  
  23.     private void LateUpdate()
  24.     {
  25.         CheckCursorPosition();
  26.  
  27.         Vector3 direction = Vector3.zero;
  28.         switch (edge)
  29.         {
  30.             case ScreenEdge.None:
  31.                 break;
  32.             case ScreenEdge.Up:
  33.                 direction = Vector3.forward;
  34.                 break;
  35.             case ScreenEdge.Down:
  36.                 direction = Vector3.back;
  37.                 break;
  38.             case ScreenEdge.Right:
  39.                 direction = Vector3.right;
  40.                 break;
  41.             case ScreenEdge.Left:
  42.                 direction = Vector3.left;
  43.                 break;
  44.             case ScreenEdge.UpRight:
  45.                 direction = Vector3.forward + Vector3.right;
  46.                 break;
  47.             case ScreenEdge.UpLeft:
  48.                 direction = Vector3.forward + Vector3.left;
  49.                 break;
  50.             case ScreenEdge.DownRight:
  51.                 direction = Vector3.back + Vector3.right;
  52.                 break;
  53.             case ScreenEdge.DownLeft:
  54.                 direction = Vector3.back + Vector3.left;
  55.                 break;
  56.             default:
  57.                 break;
  58.         }
  59.  
  60.         cameraFreeFly.moveDirection = direction;
  61.     }
  62.  
  63.     private void CheckCursorPosition()
  64.     {
  65.         float offset = 30;
  66.         edge = ScreenEdge.None;
  67.  
  68.         if (Input.mousePosition.x < offset)//У левого края
  69.         {
  70.             edge = ScreenEdge.Left;
  71.  
  72.             if (Input.mousePosition.y < offset)//У верхнего края
  73.             {
  74.                 edge = ScreenEdge.DownLeft;
  75.                 return;
  76.             }
  77.  
  78.             if (Input.mousePosition.y > Screen.height - offset)//У нижнего края
  79.             {
  80.                 edge = ScreenEdge.UpLeft;
  81.                 return;
  82.             }
  83.  
  84.             return;
  85.         }
  86.  
  87.         if (Input.mousePosition.x > Screen.width - offset)//У правого края
  88.         {
  89.             edge = ScreenEdge.Right;
  90.  
  91.             if (Input.mousePosition.y < offset)//У нижнего края
  92.             {
  93.                 edge = ScreenEdge.DownRight;
  94.                 return;
  95.             }
  96.  
  97.             if (Input.mousePosition.y > Screen.height - offset)//У верхнего края
  98.             {
  99.                 edge = ScreenEdge.UpRight;
  100.                 return;
  101.             }
  102.  
  103.             return;
  104.         }
  105.  
  106.         if (Input.mousePosition.y > Screen.height - offset)//У верхнего края
  107.         {
  108.             edge = ScreenEdge.Up;
  109.             return;
  110.         }
  111.  
  112.         if (Input.mousePosition.y < offset)//У нижнего края
  113.         {
  114.             edge = ScreenEdge.Down;
  115.             return;
  116.         }
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement