Skip to content

Commit 0188323

Browse files
author
ויויאן אומנסקי
committed
add
1 parent 10762d9 commit 0188323

20 files changed

+3561
-4412
lines changed

Assets/DraggableGroup.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using UnityEngine;
2+
3+
public class DragAndDrop : MonoBehaviour
4+
{
5+
private Vector3 offset;
6+
private Camera mainCamera;
7+
8+
private void Start()
9+
{
10+
// Cache the main camera reference
11+
mainCamera = Camera.main;
12+
}
13+
14+
private void OnMouseDown()
15+
{
16+
// Calculate offset between mouse position and object position
17+
offset = transform.position - GetMouseWorldPosition();
18+
}
19+
20+
private void OnMouseDrag()
21+
{
22+
// Move the object with the mouse while maintaining offset
23+
transform.position = GetMouseWorldPosition() + offset;
24+
}
25+
26+
private Vector3 GetMouseWorldPosition()
27+
{
28+
// Ensure the main camera exists
29+
if (mainCamera == null)
30+
{
31+
Debug.LogError("Main Camera not found!");
32+
return Vector3.zero;
33+
}
34+
35+
// Convert mouse position to world position
36+
Vector3 mousePoint = Input.mousePosition;
37+
mousePoint.z = mainCamera.WorldToScreenPoint(transform.position).z;
38+
return mainCamera.ScreenToWorldPoint(mousePoint);
39+
}
40+
}

Assets/DraggableGroup.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Images/360_F_844068098_kZjSnbJ0pSBD3LPPvkpZUqqCitQEqTTd.jpg

Lines changed: 0 additions & 3 deletions
This file was deleted.

Assets/Images/360_F_844068098_kZjSnbJ0pSBD3LPPvkpZUqqCitQEqTTd.jpg.meta

Lines changed: 0 additions & 168 deletions
This file was deleted.

Assets/Images/Horseshoe_magnet-removebg-preview.png

Lines changed: 0 additions & 3 deletions
This file was deleted.

Assets/Images/Horseshoe_magnet-removebg-preview.png.meta

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)