Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CameraController : MonoBehaviour
- {
- [SerializeField] private float movementSpeed;
- [SerializeField] private float scrollSpeed;
- [SerializeField] private float rotationSpeed;
- [SerializeField] private float maxZoom;
- [SerializeField] private float minZoom;
- [SerializeField] [Range(0f, 1f)] private float BorderThickness = 0.01f;
- [SerializeField] private float desiredDistance = 4.12f;
- [SerializeField] private float currentDistance = 4.12f;
- [SerializeField] private float zoomDampening = 5;
- public float MaxZoom { get { return maxZoom; } }
- private int mouseX, mouseY; //te nazwy są mało sugestywne, nie za bardzo wiadomo do czego są te pola
- private int up = 1; //brak odwołań, do czego to?
- private Vector3 velocity = Vector3.zero; //brak odwołań, do czego to?
- 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
- //no chyba, że miało tak być ale to raczej mało intuicyjne. Trzeba to rozwiązać inaczej.
- {
- if (GUIUtility.hotControl == 0)
- {
- if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
- {
- Rotation(inputValues);
- }
- else
- {
- Movement(inputValues);
- Zoom(inputValues);
- }
- }
- }
- public void Zoom(InputController.InputValues inputValues) //nazwy metod powinny być czasownikami. Metody coś robią i nazwa powinna sugerować co znajdę w ciele funkcji
- {
- desiredDistance -= inputValues.scroll * Time.deltaTime * scrollSpeed * Mathf.Abs(desiredDistance);
- desiredDistance = Mathf.Clamp(desiredDistance, minZoom, MaxZoom);
- currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
- currentDistance = Mathf.Clamp(currentDistance, minZoom, MaxZoom);
- transform.position = new Vector3(transform.position.x, currentDistance, transform.position.z);
- }
- public void Movement(InputController.InputValues inputValues) //nazwy metod powinny być czasownikami. Metody coś robią i nazwa powinna sugerować co znajdę w ciele funkcji
- {
- //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
- if (inputValues.xAxis != 0 || inputValues.yAxis != 0)
- {
- transform.Translate(new Vector3(inputValues.xAxis, 0, inputValues.yAxis) * movementSpeed, Space.Self);
- }
- else
- {
- CheckXAxis(inputValues);
- CheckYAxis(inputValues);
- transform.Translate(new Vector3(mouseX, 0, mouseY) * movementSpeed, Space.Self);
- }
- if (Input.GetKey(Keys.Inputs.KEYBOARD_E))
- {
- transform.Rotate(new Vector3(0, 1, 0) * rotationSpeed, Space.World);
- }
- else if (Input.GetKey(Keys.Inputs.KEYBOARD_Q))
- {
- transform.Rotate(new Vector3(0, -1, 0) * rotationSpeed, Space.World);
- }
- }
- public void Rotation(InputController.InputValues inputValues)
- {
- CheckXAxis(inputValues);
- transform.Rotate(new Vector3(0, mouseX, 0) * rotationSpeed * 2, Space.World);
- }
- //https://en.wikipedia.org/wiki/KISS_principle
- //te dwie metody są tak nie czytelne, że ja <cenzura>.
- //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.
- private void CheckXAxis(InputController.InputValues inputValues)
- {
- mouseX = inputValues.xMousePos / Screen.width < BorderThickness ? -1 : inputValues.xMousePos / Screen.width > (1 - BorderThickness) ? 1 : 0;
- }
- private void CheckYAxis(InputController.InputValues inputValues)
- {
- mouseY = (inputValues.yMousePos / Screen.height > (1 - BorderThickness)) ? 1 : (inputValues.yMousePos / Screen.height < BorderThickness) ? -1 : 0;
- }
- //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.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement