Advertisement
salahzar

DragCamera.cs

Aug 17th, 2019
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. namespace Edu3d
  7. {
  8.     public class DragCamera : MonoBehaviour
  9.     {
  10.         //#if UNITY_EDITOR
  11.  
  12.         // flag to keep track whether we are dragging or not
  13.         bool isDragging = false;
  14.  
  15.         // starting point of a camera movement
  16.         float startMouseX;
  17.         float startMouseY;
  18.  
  19.         // Camera component
  20.         Camera cam;
  21.  
  22.         public float WalkingSpeed  = 10;
  23.         public float LiftUp = 10;
  24.  
  25.  
  26.         // Use this for initialization
  27.         void Start()
  28.         {
  29.             // Get our camera component
  30.             cam = GetComponent<Camera>();
  31.         }
  32.  
  33.         // Update is called once per frame
  34.         void Update()
  35.         {
  36.            
  37.             if (Input.GetKey(KeyCode.UpArrow) || (Input.GetKey("w")))
  38.             {
  39.                 MoveForward(WalkingSpeed);
  40.             }
  41.             if (Input.GetKey(KeyCode.DownArrow) || (Input.GetKey("s")))
  42.             {
  43.                 MoveForward(-WalkingSpeed);
  44.             }
  45.             if (Input.GetKey("q") || (Input.GetKey(KeyCode.PageUp)))
  46.             {
  47.                 transform.position = transform.position + new Vector3(0, LiftUp * Time.deltaTime, 0);
  48.             }
  49.             if (Input.GetKey("z") || (Input.GetKey(KeyCode.PageDown)))
  50.             {
  51.                 transform.position = transform.position + new Vector3(0, -LiftUp * Time.deltaTime, 0);
  52.             }
  53.  
  54.  
  55.             // if we press the left button and we haven't started dragging
  56.             if (Input.GetMouseButtonDown(1) && !isDragging)
  57.             {
  58.                 // set the flag to true
  59.                 isDragging = true;
  60.  
  61.                 // save the mouse starting position
  62.                 startMouseX = Input.mousePosition.x;
  63.                 startMouseY = Input.mousePosition.y;
  64.             }
  65.             // if we are not pressing the left btn, and we were dragging
  66.             else if (Input.GetMouseButtonUp(1) && isDragging)
  67.             {
  68.                 // set the flag to false
  69.                 isDragging = false;
  70.             }
  71.         }
  72.  
  73.         private void MoveForward(float x)
  74.         {
  75.             transform.position += cam.transform.forward * x * Time.deltaTime;
  76.         }
  77.  
  78.         void LateUpdate()
  79.         {
  80.             // Check if we are dragging
  81.             if (isDragging)
  82.             {
  83.                 //Calculate current mouse position
  84.                 float endMouseX = Input.mousePosition.x;
  85.                 float endMouseY = Input.mousePosition.y;
  86.  
  87.                 //Difference (in screen coordinates)
  88.                 float diffX = endMouseX - startMouseX;
  89.                 float diffY = endMouseY - startMouseY;
  90.  
  91.                 //New center of the screen
  92.                 float newCenterX = Screen.width / 2 + diffX;
  93.                 float newCenterY = Screen.height / 2 + diffY;
  94.  
  95.                 //Get the world coordinate , this is where we want to look at
  96.                 Vector3 LookHerePoint = cam.ScreenToWorldPoint(new Vector3(newCenterX, newCenterY, cam.nearClipPlane));
  97.  
  98.                 //Make our camera look at the "LookHerePoint"
  99.                 transform.LookAt(LookHerePoint);
  100.  
  101.                 //starting position for the next call
  102.                 startMouseX = endMouseX;
  103.                 startMouseY = endMouseY;
  104.             }
  105.         }
  106.  
  107.         //#endif
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement