69 lines
1.7 KiB
C#
69 lines
1.7 KiB
C#
using UnityEngine;
|
|
|
|
public class PickupThrow : MonoBehaviour
|
|
{
|
|
public Transform holdPoint;
|
|
public float pickupRange = 3f;
|
|
public float throwForce = 10f;
|
|
|
|
private Rigidbody heldRb;
|
|
private Transform originalParent;
|
|
|
|
void Update()
|
|
{
|
|
// ËÊÌ — âçÿòè / â³äïóñòèòè
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
if (heldRb == null)
|
|
TryPickup();
|
|
else
|
|
Drop(false);
|
|
}
|
|
|
|
// ÏÊÌ — êèíóòè
|
|
if (Input.GetMouseButtonDown(1) && heldRb != null)
|
|
Drop(true);
|
|
}
|
|
|
|
void TryPickup()
|
|
{
|
|
if (Physics.Raycast(Camera.main.transform.position,
|
|
Camera.main.transform.forward, out RaycastHit hit, pickupRange))
|
|
{
|
|
if (!hit.collider.CompareTag("Pickable")) return;
|
|
|
|
heldRb = hit.collider.attachedRigidbody;
|
|
if (heldRb == null) return;
|
|
|
|
originalParent = heldRb.transform.parent;
|
|
|
|
// Âèìèêàºìî ô³çèêó
|
|
heldRb.isKinematic = true;
|
|
heldRb.useGravity = false;
|
|
|
|
// Ïðèêð³ïëþºìî äî ðóêè
|
|
heldRb.transform.SetParent(holdPoint);
|
|
heldRb.transform.localPosition = Vector3.zero;
|
|
heldRb.transform.localRotation = Quaternion.identity;
|
|
}
|
|
}
|
|
|
|
void Drop(bool throwIt)
|
|
{
|
|
// ³äêð³ïëþºìî
|
|
heldRb.transform.SetParent(originalParent);
|
|
|
|
// Âìèêàºìî ô³çèêó
|
|
heldRb.isKinematic = false;
|
|
heldRb.useGravity = true;
|
|
|
|
if (throwIt)
|
|
{
|
|
heldRb.AddForce(Camera.main.transform.forward * throwForce,
|
|
ForceMode.VelocityChange);
|
|
}
|
|
|
|
heldRb = null;
|
|
}
|
|
}
|