Files
2026-03-17 13:40:09 +02:00

157 lines
4.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CustomCursor : MonoBehaviour
{
[Header("Курсори")]
[SerializeField] private Texture2D normalCursor;
[SerializeField] private Texture2D targetCursor;
[SerializeField] private Vector2 hotspot = new Vector2(16, 16);
[Header("Підняття")]
[SerializeField] private Camera cameraMain;
[SerializeField] private float pickupDistance = 5f;
[SerializeField] private float holdDistance = 3.4f;
[SerializeField] private Vector3 holdOffset = new Vector3(0, 0.5f, 0);
[SerializeField] private float moveSpeed = 15f;
[Header("Кидання")]
[SerializeField] private float throwForwardForce = 5f;
[SerializeField] private float throwUpForce = 3f;
[SerializeField] private KeyCode throwKey = KeyCode.Mouse0;
private GameObject _heldObject;
private Rigidbody _heldObjectRb;
private AirTrailController _currentTrail;
private void Start()
{
if (cameraMain == null)
cameraMain = Camera.main;
Cursor.SetCursor(normalCursor, hotspot, CursorMode.Auto);
}
private void Update()
{
Ray ray = cameraMain.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (_heldObject != null)
{
if (Input.GetKeyDown(KeyCode.E))
{
PlaceObject();
return;
}
if (Input.GetKeyDown(throwKey))
{
ThrowObject();
return;
}
}
else
{
if (Physics.Raycast(ray, out hit, pickupDistance))
{
if (hit.collider.CompareTag("Target"))
{
Cursor.SetCursor(targetCursor, hotspot, CursorMode.Auto);
if (Input.GetKeyDown(KeyCode.E))
PickupObject(hit.collider.gameObject);
}
else
{
Cursor.SetCursor(normalCursor, hotspot, CursorMode.Auto);
}
}
else
{
Cursor.SetCursor(normalCursor, hotspot, CursorMode.Auto);
}
}
}
private void FixedUpdate()
{
if (_heldObject != null)
HoldObjectAtCursor();
}
private void PickupObject(GameObject obj)
{
_heldObject = obj;
_heldObjectRb = obj.GetComponent<Rigidbody>();
_heldObjectRb.useGravity = false;
_heldObjectRb.velocity = Vector3.zero;
_heldObjectRb.drag = 10f;
_heldObjectRb.angularDrag = 10f;
_currentTrail = _heldObject.GetComponentInChildren<AirTrailController>();
if (_currentTrail != null)
{
_currentTrail.Deactivate();
}
}
private void PlaceObject()
{
bool isMoonObject = _heldObject.GetComponent<MoonGravity>() != null;
_heldObjectRb.useGravity = !isMoonObject;
_heldObjectRb.drag = 0f;
_heldObjectRb.angularDrag = 0.05f;
_heldObjectRb.velocity = Vector3.zero;
if (_currentTrail != null)
{
_currentTrail.Deactivate();
}
_heldObject = null;
_heldObjectRb = null;
_currentTrail = null;
}
private void HoldObjectAtCursor()
{
Ray ray = cameraMain.ScreenPointToRay(Input.mousePosition);
Vector3 targetPos = ray.GetPoint(holdDistance) + holdOffset;
Vector3 dir = targetPos - _heldObject.transform.position;
_heldObjectRb.velocity = dir * moveSpeed;
}
private void ThrowObject()
{
if (_currentTrail != null)
{
Vector3 tailStart = _heldObject.transform.position - cameraMain.transform.forward * 0.3f;
_currentTrail.Activate(tailStart);
}
ObjectPhysicsInfo earthInfo = _heldObject.GetComponent<ObjectPhysicsInfo>();
if (earthInfo != null) earthInfo.OnRelease();
MoonObjectInfo moonInfo = _heldObject.GetComponent<MoonObjectInfo>();
if (moonInfo != null) moonInfo.OnRelease();
bool isMoonObject = _heldObject.GetComponent<MoonGravity>() != null;
_heldObjectRb.useGravity = !isMoonObject;
_heldObjectRb.drag = 0f;
_heldObjectRb.angularDrag = 0.05f;
Vector3 throwDir = cameraMain.transform.forward * throwForwardForce + Vector3.up * throwUpForce;
_heldObjectRb.velocity = throwDir;
_heldObject = null;
_heldObjectRb = null;
_currentTrail = null;
}
}