Screen edge detection of unity camera moving

Time:2021-2-27

This example for you to share the unity camera moving screen edge detection of the specific code, for your reference, the specific content is as follows

Function:

Similar to lol Red Alert camera movement mode.

The mouse moves to the edge of the screen and the camera moves with it.

Of course, we can add a little bit of detail, such as mouse pointer change, scroll wheel push to widen the field of vision, middle key translation field of vision, etc. (I didn’t do it).

design sketch:

Here we do the visualization data (we can see the limited screen distance), and the detection distance is in the wireframe.

code:

Copy the script and mount it directly on the camera.

using UnityEngine;
 
/// <summary>
///Camera edge movement
/// </summary>
[RequireComponent(typeof(Camera))]
public class CameraScreenEdgeMove :MonoBehaviour
{
 
 
 [header ("use edge move")]
 public bool isUseMoveOnScreenEdge = true;
 
 /// <summary>
 ///Open debug
 /// </summary>
 public bool isDebugScreenEdge = true;
 
 //Moving speed
 public float moveSpeed = 1f;
 
 /// <summary>
 ///Start moving the camera as far as the edge of the screen
 /// </summary>
 public int ScreenEdgeSize = 20;
 
 private bool MoveUp;
 private bool MoveDown;
 private bool MoveRight;
 private bool MoveLeft;
 
 private Rect RigthRect;
 private Rect UpRect;
 private Rect DownRect;
 private Rect LeftRect;
 
 private Material mat;
 private Vector3 dir = Vector3.zero;
 
 private void Start()
 {
 CreateLineMaterial();
 }
 
 private void Update()
 {
 if (isUseMoveOnScreenEdge)
 {
  UpRect = new Rect(1f, Screen.height - ScreenEdgeSize, Screen.width, ScreenEdgeSize);
  DownRect = new Rect(1f, 1f, Screen.width, ScreenEdgeSize);
 
  LeftRect = new Rect(1f, 1f, ScreenEdgeSize, Screen.height);
  RigthRect = new Rect(Screen.width - ScreenEdgeSize, 1f, ScreenEdgeSize, Screen.height);
 
 
  MoveUp = (UpRect.Contains(Input.mousePosition));
  MoveDown = (DownRect.Contains(Input.mousePosition));
 
  MoveLeft = (LeftRect.Contains(Input.mousePosition));
  MoveRight = (RigthRect.Contains(Input.mousePosition));
 
  dir.z = MoveUp ? 1 : MoveDown ? -1 : 0;
  dir.x = MoveLeft ? -1 : MoveRight ? 1 : 0;
 
  transform.position = Vector3.Lerp(transform.position, transform.position + dir * moveSpeed,Time.deltaTime);
 
 }
 }
 
 
 void CreateLineMaterial()
 {
 if (!mat)
 {
  Shader shader = Shader.Find("Hidden/Internal-Colored");
  mat = new Material(shader);
  mat.hideFlags = HideFlags.HideAndDontSave;
  mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  mat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
  mat.SetInt("_ZWrite", 0);
 }
 }
 
 void OnPostRender()
 {
 if (isUseMoveOnScreenEdge && isDebugScreenEdge)
 {
  DrawRect(UpRect, MoveUp, Color.cyan, Color.red); 
  DrawRect(DownRect, MoveDown, Color.green, Color.red); 
  DrawRect(LeftRect, MoveLeft, Color.yellow, Color.red); 
  DrawRect(RigthRect, MoveRight, Color.blue, Color.red); 
 }
 }
 
 private void DrawRect(Rect rect, bool isMouseEnter, Color normalColor, Color HeighLightColor)
 {
 if (isMouseEnter)
 {
  DrawScreenRect(rect, HeighLightColor);
 }
 else
 {
  DrawScreenRect(rect, normalColor);
 }
 }
 
 private void DrawScreenRect(Rect rect, Color color)
 {
 GL.LoadOrtho();
 GL.Begin(GL.LINES);
 {
  mat.SetPass(0);
  GL.Color(color);
  GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
  GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
 
  GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
  GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
 
  GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
  GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
 
  GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
  GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
  
 }
 GL.End();
 }
 
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.