/
Bruhh
/
Simulator
Обзор
Документация
Войти
/
Bruhh
/
Simulator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
PCBuilderController.cs
185 строк
5 KB
Bruhh
upload files
04 дек 2025, 08:25
04 дек 2025, 08:25
838886c
Код
Авторство
О чём код?
using UnityEngine; public class PCBuilderController : MonoBehaviour { [Header("Movement")] public float moveSpeed = 3f; public float rotationSpeed = 200f; public float reachDistance = 1.5f; [Header("Interaction")] public Transform holdPoint; public LayerMask componentLayer; public LayerMask socketLayer; public float placementOffset = 0.1f; private Rigidbody rb; private Camera mainCamera; private PCComponent heldComponent; private bool isHolding = false; private PCSocket highlightedSocket; private void Awake() { rb = GetComponent<Rigidbody>(); rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotation; mainCamera = Camera.main; } private void CreateHoldPoint() { // Создаем новый GameObject GameObject holdPoint = new GameObject("HoldPoint"); // Делаем его дочерним по отношению к игроку holdPoint.transform.SetParent(transform); // Устанавливаем позицию относительно игрока holdPoint.transform.localPosition = new Vector3(0f, 1f, 0.5f); // Сохраняем ссылку для использования в коде this.holdPoint = holdPoint.transform; } private void Update() { HandleMovement(); HandleInteraction(); UpdateHighlight(); TryPlaceComponent(); } private void HandleMovement() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Vector3 movement = transform.forward * vertical + transform.right * horizontal; movement = movement.normalized * moveSpeed * Time.deltaTime; rb.MovePosition(rb.position + movement); float rotation = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime; transform.Rotate(0, rotation, 0); } private void HandleInteraction() { if (Input.GetMouseButtonDown(0)) { if (!isHolding) { TryPickupComponent(); } else { TryPlaceComponent(); } } if (Input.GetMouseButtonDown(1) && isHolding) { DropComponent(); } } private void TryPickupComponent() { Ray ray = mainCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit, reachDistance, componentLayer)) { PCComponent component = hit.collider.GetComponent<PCComponent>(); if (component != null && !component.IsInstalled) { GrabComponent(component); } } } private void GrabComponent(PCComponent component) { heldComponent = component; isHolding = true; heldComponent.GetComponent<Rigidbody>().isKinematic = true; heldComponent.transform.SetParent(holdPoint); heldComponent.transform.localPosition = Vector3.zero; heldComponent.transform.localRotation = Quaternion.identity; heldComponent.OnPickedUp(); } private void TryPlaceComponent() { if (highlightedSocket != null && heldComponent != null) { if (highlightedSocket.CanAcceptComponent(heldComponent)) { highlightedSocket.InstallComponent(heldComponent); heldComponent.OnInstalled(); heldComponent = null; isHolding = false; return; } } // Если не над подходящим сокетом, просто отпускаем компонент DropComponent(); } private void PlaceComponent(PCSocket socket) { socket.InstallComponent(heldComponent); heldComponent.OnInstalled(); heldComponent = null; isHolding = false; PCBuilderManager.Instance.CheckAssemblyProgress(); } private void DropComponent() { if (heldComponent == null) return; heldComponent.GetComponent<Rigidbody>().isKinematic = false; heldComponent.transform.SetParent(null); heldComponent.OnDropped(); heldComponent = null; isHolding = false; } private void UpdateHighlight() { if (isHolding) { Ray ray = mainCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit, reachDistance, socketLayer)) { PCSocket socket = hit.collider.GetComponent<PCSocket>(); if (socket != null && socket.CanAcceptComponent(heldComponent)) { if (highlightedSocket != socket) { if (highlightedSocket != null) highlightedSocket.OnStopHighlight(); highlightedSocket = socket; highlightedSocket.OnHighlight(); } return; } } } if (highlightedSocket != null) { highlightedSocket.OnStopHighlight(); highlightedSocket = null; } } }