Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Mouse navigation for Unity
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class WorldNavigation : MonoBehaviour {
- public Camera cam;
- private bool isPanning;
- private bool isRotating;
- private bool isTilting;
- private bool isZooming;
- private Vector3 touchStart;
- public float groundZ = 0;
- //rotation params
- public float speedH = 2.0f;
- public float speedV = 2.0f;
- private float yaw;
- private float pitch;
- //tilting variables
- private Vector3 pivot;
- private Vector3 rotationAxis;
- private float y_rotate;
- private float x_rotate;
- public float rotationSpeed = 10f;
- // Update is called once per frame
- void Update () {
- // --GETTING INPUT--
- // Left mouse button: Panning
- if (Input.GetMouseButtonDown (0)) {
- touchStart = mouseWorldPosition (groundZ);
- isPanning = true;
- };
- if (Input.GetMouseButton (0)) {
- Vector3 direction = touchStart - mouseWorldPosition (groundZ);
- cam.transform.position += direction;
- }
- // Right mouse button: Rotating
- if (Input.GetMouseButtonDown (1)) {
- Vector3 angles = cam.transform.eulerAngles;
- yaw = angles.y;
- pitch = angles.x;
- isRotating = true;
- }
- // Middle mouse button: Tilting
- float currentCamX = cam.transform.position.x;
- float currentCamZ = cam.transform.position.z;
- if (cam.transform.position.y < 0.5f) {
- cam.transform.position = new Vector3 (currentCamX, 0.5f, currentCamZ);
- }
- if (Input.GetMouseButtonDown (2)) {
- pivot = centerScreenPosition (groundZ);
- isTilting = true;
- }
- // Middle Mouse Scroll
- if (Input.GetAxis ("Mouse ScrollWheel") != 0) {
- isZooming = true;
- }
- // canceling all functions
- if (!Input.GetMouseButton (0)) isPanning = false;
- if (!Input.GetMouseButton (1)) isRotating = false;
- if (!Input.GetMouseButton (2)) isTilting = false;
- if (Input.GetAxis ("Mouse ScrollWheel") == 0) isZooming = false;
- }
- // LATE UPDATE
- void LateUpdate () {
- //Right Button: Camera Rotation
- if (isRotating) {
- yaw += speedH * Input.GetAxis ("Mouse X");
- pitch -= speedV * Input.GetAxis ("Mouse Y");
- // Clamp pitch:
- pitch = Mathf.Clamp (pitch, -90f, 90f);
- Quaternion rotation = Quaternion.Euler (pitch, yaw, 0);
- cam.transform.rotation = rotation;
- }
- //middle mouse: tilting
- if (isTilting) {
- y_rotate = Input.GetAxis ("Mouse X") * rotationSpeed;
- x_rotate = Input.GetAxis ("Mouse Y") * rotationSpeed;
- //cam.transform.RotateAround (pivot, Vector3.left, rotationAngleOnX);
- OrbitCamera (cam, pivot, y_rotate, x_rotate);
- }
- // mouse scrollwheel: zooming
- if (isZooming) {
- Vector3 desiredPosition;
- desiredPosition = mouseWorldPosition (groundZ);
- float distance = Vector3.Distance (desiredPosition, transform.position);
- Vector3 direction = Vector3.Normalize (desiredPosition - transform.position) * (distance * Input.GetAxis ("Mouse ScrollWheel"));
- transform.position += direction;
- }
- }
- // Raycasting Mouse Position for Panning
- private Vector3 mouseWorldPosition (float z) {
- Ray mousePos = cam.ScreenPointToRay (Input.mousePosition);
- Plane ground = new Plane (Vector3.forward, new Vector3 (0, 0, z));
- float distance;
- ground.Raycast (mousePos, out distance);
- return mousePos.GetPoint (distance);
- }
- // Raycasting center of screen for Tilting
- private Vector3 centerScreenPosition (float z) {
- //Ray mousePos = cam.ScreenPointToRay(Input.mousePosition);
- Ray centerScreen = cam.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0f));
- Plane ground = new Plane (Vector3.forward, new Vector3 (0, 0, z));
- float distance;
- //ground.Raycast(mousePos, out distance);
- ground.Raycast (centerScreen, out distance);
- //return mousePos.GetPoint(distance);
- return centerScreen.GetPoint (distance);
- }
- // calculating orbit camera
- public void OrbitCamera (Camera cam, Vector3 target, float y_rotate, float x_rotate) {
- Vector3 angles = transform.eulerAngles;
- angles.z = 0;
- cam.transform.eulerAngles = angles;
- cam.transform.RotateAround (target, Vector3.up, y_rotate);
- cam.transform.RotateAround (target, Vector3.left, x_rotate);
- cam.transform.LookAt (target);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement