salahzar icon

DragCamera.cs

salahzar | PRO | 08/17/19 03:50:25 PM UTC | 0 ⭐ | 1105 👁️ | Never ⏰ | []
C# |

3.38 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace Edu3d
{
    public class DragCamera : MonoBehaviour
    {
        //#if UNITY_EDITOR
 
        // flag to keep track whether we are dragging or not
        bool isDragging = false;
 
        // starting point of a camera movement
        float startMouseX;
        float startMouseY;
 
        // Camera component
        Camera cam;
 
        public float WalkingSpeed  = 10;
        public float LiftUp = 10;
 
 
        // Use this for initialization
        void Start()
        {
            // Get our camera component
            cam = GetComponent<Camera>();
        }
 
        // Update is called once per frame
        void Update()
        {
            
            if (Input.GetKey(KeyCode.UpArrow) || (Input.GetKey("w")))
            {
                MoveForward(WalkingSpeed);
            }
            if (Input.GetKey(KeyCode.DownArrow) || (Input.GetKey("s")))
            {
                MoveForward(-WalkingSpeed);
            }
            if (Input.GetKey("q") || (Input.GetKey(KeyCode.PageUp)))
            {
                transform.position = transform.position + new Vector3(0, LiftUp * Time.deltaTime, 0);
            }
            if (Input.GetKey("z") || (Input.GetKey(KeyCode.PageDown)))
            {
                transform.position = transform.position + new Vector3(0, -LiftUp * Time.deltaTime, 0);
            }
 
 
            // if we press the left button and we haven't started dragging
            if (Input.GetMouseButtonDown(1) && !isDragging)
            {
                // set the flag to true
                isDragging = true;
 
                // save the mouse starting position
                startMouseX = Input.mousePosition.x;
                startMouseY = Input.mousePosition.y;
            }
            // if we are not pressing the left btn, and we were dragging
            else if (Input.GetMouseButtonUp(1) && isDragging)
            {
                // set the flag to false
                isDragging = false;
            }
        }
 
        private void MoveForward(float x)
        {
            transform.position += cam.transform.forward * x * Time.deltaTime;
        }
 
        void LateUpdate()
        {
            // Check if we are dragging
            if (isDragging)
            {
                //Calculate current mouse position
                float endMouseX = Input.mousePosition.x;
                float endMouseY = Input.mousePosition.y;
 
                //Difference (in screen coordinates)
                float diffX = endMouseX - startMouseX;
                float diffY = endMouseY - startMouseY;
 
                //New center of the screen
                float newCenterX = Screen.width / 2 + diffX;
                float newCenterY = Screen.height / 2 + diffY;
 
                //Get the world coordinate , this is where we want to look at
                Vector3 LookHerePoint = cam.ScreenToWorldPoint(new Vector3(newCenterX, newCenterY, cam.nearClipPlane));
 
                //Make our camera look at the "LookHerePoint"
                transform.LookAt(LookHerePoint);
 
                //starting position for the next call
                startMouseX = endMouseX;
                startMouseY = endMouseY;
            }
        }
 
        //#endif
    }
}

Comments