File size: 8,464 Bytes
f930a98 c826805 1eb49ab c826805 1eb49ab 8ef761f 1eb49ab c826805 1eb49ab ff67770 c826805 8ef761f f930a98 ff67770 8ef761f 1eb49ab c826805 ff67770 8ef761f c826805 1eb49ab 8ef761f c826805 1eb49ab c826805 f930a98 c826805 ff67770 f930a98 c826805 ff67770 f930a98 ff67770 8ef761f c826805 8ef761f c826805 8ef761f 1eb49ab c826805 1eb49ab 8ef761f c826805 1eb49ab 8ef761f f930a98 c826805 ff67770 f930a98 ff67770 f930a98 ff67770 f930a98 ff67770 f930a98 ff67770 f930a98 6e8f8e9 85514dd f930a98 c826805 8ef761f 1eb49ab 8ef761f 1eb49ab c826805 1eb49ab c826805 8ef761f c826805 ff67770 1eb49ab 8ef761f ff67770 1eb49ab ff67770 c826805 ff67770 1eb49ab 8ef761f 1eb49ab c826805 1eb49ab c826805 1eb49ab c826805 8ef761f c826805 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | using System.Collections;
using UnityEngine;
public class PickUpScript : MonoBehaviour
{
public static PickUpScript instance;
public GameObject player;
public Transform holdPos;
[Header("Camera & Controls")]
[Tooltip("Drag the PlayerCamController script here.")]
public PlayerCamController cameraLookScript;
[Header("Physics Settings")]
public float throwForce = 500f;
public float pickUpRange = 5f;
public string holdLayerName = "HoldLayer";
private int holdLayerIndex;
[Tooltip("How fast the object rotates with the mouse")]
public float rotationSensitivity = 2f;
[Header("UI Panels")]
public CanvasGroup holdUIGroup;
public CanvasGroup rotateUIGroup;
public float uiFadeSpeed = 8f;
[Header("Animation Settings")]
public float pickupSpeed = 0.2f;
public float takeSpeed = 0.3f;
public GameObject heldObj;
private Rigidbody heldObjRb;
private bool canDrop = true;
private bool isAnimating = false;
private Vector3 localCenterOffset;
private float dropCooldown = 0f;
private float targetHoldAlpha = 0f;
private float targetRotateAlpha = 0f;
private void Awake()
{
instance = this;
if (holdUIGroup != null) holdUIGroup.alpha = 0f;
if (rotateUIGroup != null) rotateUIGroup.alpha = 0f;
}
void Start()
{
holdLayerIndex = LayerMask.NameToLayer(holdLayerName);
if (holdLayerIndex == -1)
{
Debug.LogError($"[PickUpScript] Layer '{holdLayerName}' does not exist! Defaulting to layer 0.");
holdLayerIndex = 0;
}
}
void Update()
{
if (heldObj != null && !isAnimating)
{
MoveObject();
RotateObject();
// 1. DROP ITEM ('X')
if (Input.GetKeyDown(KeyCode.X) && canDrop == true)
{
StopClipping();
DropObject();
}
// 2. TAKE ITEM ('E')
else if (Input.GetKeyDown(KeyCode.E) && canDrop == true)
{
TakeObject();
}
// 3. THROW ITEM (Left Click)
else if (Input.GetKeyDown(KeyCode.Mouse0) && canDrop == true)
{
StopClipping();
ThrowObject();
}
}
// Smooth UI Fading Logic
if (holdUIGroup != null)
{
holdUIGroup.alpha = Mathf.Lerp(holdUIGroup.alpha, targetHoldAlpha, Time.deltaTime * uiFadeSpeed);
}
if (rotateUIGroup != null)
{
rotateUIGroup.alpha = Mathf.Lerp(rotateUIGroup.alpha, targetRotateAlpha, Time.deltaTime * uiFadeSpeed);
}
}
public void PickUpObject(GameObject pickUpObj)
{
if (Time.time < dropCooldown) return;
Rigidbody rb = pickUpObj.GetComponent<Rigidbody>();
if (rb == null) rb = pickUpObj.GetComponentInChildren<Rigidbody>();
if (rb != null)
{
heldObj = pickUpObj;
heldObjRb = rb;
Renderer[] renderers = heldObj.GetComponentsInChildren<Renderer>();
if (renderers.Length > 0)
{
Bounds combinedBounds = renderers[0].bounds;
for (int i = 1; i < renderers.Length; i++)
{
combinedBounds.Encapsulate(renderers[i].bounds);
}
localCenterOffset = heldObj.transform.InverseTransformPoint(combinedBounds.center);
}
else
{
localCenterOffset = Vector3.zero;
}
heldObjRb.isKinematic = true;
heldObj.transform.parent = holdPos.transform;
SetLayerRecursively(heldObj, holdLayerIndex);
Collider[] colliders = heldObj.GetComponentsInChildren<Collider>();
foreach (Collider col in colliders)
{
col.enabled = false;
}
targetHoldAlpha = 1f;
targetRotateAlpha = 0f;
StartCoroutine(AnimatePickup());
}
}
private IEnumerator AnimatePickup()
{
isAnimating = true;
float timeElapsed = 0f;
Vector3 startPos = heldObj.transform.position;
while (timeElapsed < pickupSpeed)
{
timeElapsed += Time.deltaTime;
float t = timeElapsed / pickupSpeed;
Vector3 targetPos = holdPos.position - heldObj.transform.TransformDirection(localCenterOffset);
heldObj.transform.position = Vector3.Lerp(startPos, targetPos, t);
yield return null;
}
isAnimating = false;
}
void TakeObject()
{
dropCooldown = Time.time + 0.2f;
StartCoroutine(AnimateTake());
}
private IEnumerator AnimateTake()
{
isAnimating = true;
HideAllUI();
float timeElapsed = 0f;
Vector3 startPos = heldObj.transform.position;
Vector3 startScale = heldObj.transform.localScale;
while (timeElapsed < takeSpeed)
{
timeElapsed += Time.deltaTime;
float t = timeElapsed / takeSpeed;
Vector3 targetPos = holdPos.position - holdPos.right * 0.5f - holdPos.up * 0.6f;
heldObj.transform.position = Vector3.Lerp(startPos, targetPos, t);
heldObj.transform.localScale = Vector3.Lerp(startScale, Vector3.zero, t);
yield return null;
}
InventoryManager.instance.AddToInventory(heldObj);
// Destroy(heldObj);
heldObj = null;
isAnimating = false;
}
void DropObject()
{
dropCooldown = Time.time + 0.2f;
HideAllUI();
Collider[] colliders = heldObj.GetComponentsInChildren<Collider>();
foreach (Collider col in colliders)
{
col.enabled = true;
}
SetLayerRecursively(heldObj, 0);
heldObjRb.isKinematic = false;
heldObj.transform.parent = null;
heldObj = null;
}
void ThrowObject()
{
dropCooldown = Time.time + 0.2f;
HideAllUI();
Collider[] colliders = heldObj.GetComponentsInChildren<Collider>();
foreach (Collider col in colliders)
{
col.enabled = true;
}
SetLayerRecursively(heldObj, 0);
heldObjRb.isKinematic = false;
heldObj.transform.parent = null;
heldObjRb.AddForce(transform.forward * throwForce);
heldObj = null;
}
void MoveObject()
{
heldObj.transform.position = holdPos.position - heldObj.transform.TransformDirection(localCenterOffset);
}
void RotateObject()
{
if (Input.GetKey(KeyCode.R))
{
canDrop = false; // Disables Throw/Drop/Take while inspecting
if (cameraLookScript != null) cameraLookScript.enabled = false;
targetHoldAlpha = 0f;
targetRotateAlpha = 1f;
// Hold LEFT CLICK to rotate Horizontally
if (Input.GetMouseButton(0))
{
float xAxisRotation = Input.GetAxis("Mouse X") * rotationSensitivity;
heldObj.transform.Rotate(transform.up, -xAxisRotation, Space.World);
}
// Hold RIGHT CLICK to rotate Vertically
if (Input.GetMouseButton(1))
{
float yAxisRotation = Input.GetAxis("Mouse Y") * rotationSensitivity;
heldObj.transform.Rotate(transform.right, yAxisRotation, Space.World);
}
}
else
{
canDrop = true; // Enables actions again
if (cameraLookScript != null) cameraLookScript.enabled = true;
targetHoldAlpha = 1f;
targetRotateAlpha = 0f;
}
}
void StopClipping()
{
var clipRange = Vector3.Distance(heldObj.transform.position, transform.position);
if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, clipRange))
{
heldObj.transform.position = hit.point + (hit.normal * 0.1f);
}
}
private void SetLayerRecursively(GameObject obj, int newLayer)
{
obj.layer = newLayer;
foreach (Transform child in obj.transform)
{
SetLayerRecursively(child.gameObject, newLayer);
}
}
private void HideAllUI()
{
targetHoldAlpha = 0f;
targetRotateAlpha = 0f;
}
} |