Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraController : MonoBehaviour
  6. {
  7.     [SerializeField] private float movementSpeed;
  8.     [SerializeField] private float scrollSpeed;
  9.     [SerializeField] private float rotationSpeed;
  10.     [SerializeField] private float maxZoom;
  11.     [SerializeField] private float minZoom;
  12.     [SerializeField] [Range(0f, 1f)] private float BorderThickness = 0.01f;
  13.     [SerializeField] private float desiredDistance = 4.12f;
  14.     [SerializeField] private float currentDistance = 4.12f;
  15.     [SerializeField] private float zoomDampening = 5;
  16.     public float MaxZoom { get { return maxZoom; } }
  17.  
  18.     private int mouseX, mouseY; //te nazwy są mało sugestywne, nie za bardzo wiadomo do czego są te pola
  19.     private int up = 1; //brak odwołań, do czego to?
  20.     private Vector3 velocity = Vector3.zero; //brak odwołań, do czego to?
  21.  
  22.     public void UpdateCamera(InputController.InputValues inputValues) //te metoda się buguje. Wystarczy puścić zoom, nacisnąć klawisz myszy(zoom wtedy staje) a po puszczeniu leci dalej
  23.         //no chyba, że miało tak być ale to raczej mało intuicyjne. Trzeba to rozwiązać inaczej.
  24.     {
  25.         if (GUIUtility.hotControl == 0)
  26.         {            
  27.             if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
  28.             {
  29.                 Rotation(inputValues);
  30.             }
  31.             else
  32.             {
  33.                 Movement(inputValues);
  34.                 Zoom(inputValues);
  35.             }
  36.         }
  37.     }
  38.  
  39.     public void Zoom(InputController.InputValues inputValues) //nazwy metod powinny być czasownikami. Metody coś robią i nazwa powinna sugerować co znajdę w ciele funkcji
  40.     {
  41.         desiredDistance -= inputValues.scroll * Time.deltaTime * scrollSpeed * Mathf.Abs(desiredDistance);
  42.         desiredDistance = Mathf.Clamp(desiredDistance, minZoom, MaxZoom);
  43.  
  44.         currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
  45.         currentDistance = Mathf.Clamp(currentDistance, minZoom, MaxZoom);
  46.  
  47.         transform.position = new Vector3(transform.position.x, currentDistance, transform.position.z);
  48.     }
  49.  
  50.     public void Movement(InputController.InputValues inputValues) //nazwy metod powinny być czasownikami. Metody coś robią i nazwa powinna sugerować co znajdę w ciele funkcji
  51.     {
  52.         //ja bym rozbił tą metodę na dwie metody, jedna do obsługi myszy, druga do obsługi klawiatury. W tym momencie trzeba się chwilę zastanowić, żeby skumać co tu się w ogóle dzieje
  53.         if (inputValues.xAxis != 0 || inputValues.yAxis != 0)
  54.         {
  55.             transform.Translate(new Vector3(inputValues.xAxis, 0, inputValues.yAxis) * movementSpeed, Space.Self);
  56.         }
  57.         else
  58.         {
  59.             CheckXAxis(inputValues);
  60.             CheckYAxis(inputValues);
  61.             transform.Translate(new Vector3(mouseX, 0, mouseY) * movementSpeed, Space.Self);
  62.         }
  63.  
  64.  
  65.         if (Input.GetKey(Keys.Inputs.KEYBOARD_E))
  66.         {
  67.             transform.Rotate(new Vector3(0, 1, 0) * rotationSpeed, Space.World);
  68.         }
  69.         else if (Input.GetKey(Keys.Inputs.KEYBOARD_Q))
  70.         {
  71.             transform.Rotate(new Vector3(0, -1, 0) * rotationSpeed, Space.World);
  72.         }
  73.     }
  74.     public void Rotation(InputController.InputValues inputValues)
  75.     {
  76.         CheckXAxis(inputValues);
  77.         transform.Rotate(new Vector3(0, mouseX, 0) * rotationSpeed * 2, Space.World);
  78.     }
  79.  
  80.     //https://en.wikipedia.org/wiki/KISS_principle
  81.     //te dwie metody są tak nie czytelne, że ja <cenzura>.
  82.     //zagnieżdzanie tego typu instrukcji w sobie to proszenie się o guza, chyba, że ktośto z neta zakosił i trochę przerobił to też lekko słaba praktyka. Do poprawy.
  83.     private void CheckXAxis(InputController.InputValues inputValues)
  84.     {
  85.         mouseX = inputValues.xMousePos / Screen.width < BorderThickness ? -1 : inputValues.xMousePos / Screen.width > (1 - BorderThickness) ? 1 : 0;
  86.     }
  87.     private void CheckYAxis(InputController.InputValues inputValues)
  88.     {
  89.         mouseY = (inputValues.yMousePos / Screen.height > (1 - BorderThickness)) ? 1 : (inputValues.yMousePos / Screen.height < BorderThickness) ? -1 : 0;
  90.     }
  91.  
  92.     //do powyższego -> może byłoby lepiej jakby zamiast tych nic nie mówiących stałych był wstawione jakieś klucze, które sugerują czy te stałe są ale i tak i tak do poprawy.
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement