2025-03-24 07:53:48 +01:00

52 lines
1.8 KiB
C#

using UnityEngine;
public class FreeCamera : MonoBehaviour
{
public float sensitivity = 2.0f; // ×óâñòâèòåëüíîñòü ìûøè
public float moveSpeed = 5.0f; // Ñêîðîñòü ïåðåìåùåíèÿ
public float fastMoveSpeed = 10.0f; // Óñêîðåííàÿ ñêîðîñòü ïåðåìåùåíèÿ
public float smoothTime = 0.12f; // Âðåìÿ ñãëàæèâàíèÿ
private bool isRotating = false;
private float yaw = 0.0f;
private float pitch = 0.0f;
private Vector3 smoothMoveVelocity;
void Update()
{
if (Input.GetMouseButtonDown(1)) // Ïðîâåðÿåì íàæàòèå ïðàâîé êíîïêè ìûøè
{
isRotating = true;
}
if (Input.GetMouseButtonUp(1)) // Ïðîâåðÿåì îòïóñêàíèå ïðàâîé êíîïêè ìûøè
{
isRotating = false;
}
if (isRotating)
{
// Âðàùåíèå êàìåðû ïî îñÿì ìûøè
float rotateHorizontal = Input.GetAxis("Mouse X") * sensitivity;
float rotateVertical = Input.GetAxis("Mouse Y") * sensitivity;
yaw += rotateHorizontal;
pitch -= rotateVertical;
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
// Ïåðåìåùåíèå êàìåðû
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
float moveUpDown = Input.GetAxis("Jump");
Vector3 moveDirection = (transform.forward * moveVertical) + (transform.right * moveHorizontal) + (transform.up * moveUpDown);
float currentMoveSpeed = Input.GetKey(KeyCode.LeftShift) ? fastMoveSpeed : moveSpeed; // Ïðîâåðêà íàæàòèÿ Shift
Vector3 targetVelocity = moveDirection * currentMoveSpeed;
// Ñãëàæèâàíèå ïåðåìåùåíèÿ
transform.position = Vector3.SmoothDamp(transform.position, transform.position + targetVelocity, ref smoothMoveVelocity, smoothTime);
}
}