| | |
| | | { |
| | | public float minX = -2f; // 최소 X 위치 |
| | | public float maxX = 2f; // 최대 X 위치 |
| | | |
| | | [Range(0.01f, 0.1f )] |
| | | [Range(0.01f, 0.1f)] |
| | | public float Sensitivity = 0.1f; |
| | | |
| | | private Vector3 offset; |
| | | private float startX; |
| | | private bool isDragging = false; |
| | | private Camera mainCamera; |
| | | private Vector3 lastMousePosition; |
| | | private Vector3 moveDirection; |
| | | |
| | | |
| | | |
| | | void Start() |
| | | { |
| | | mainCamera = Camera.main; |
| | |
| | | |
| | | public void OnDrag() |
| | | { |
| | | startX = transform.localPosition.x; |
| | | isDragging = true; |
| | | offset = transform.localPosition - GetMouseWorldPos(); |
| | | lastMousePosition = Input.mousePosition; |
| | | |
| | | // 드래그 시작 시 이동 방향 결정 |
| | | Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); |
| | | Vector3 directionToChair = transform.position - ray.origin; |
| | | moveDirection = Vector3.Cross(ray.direction, Vector3.up).normalized; |
| | | |
| | | if (Vector3.Dot(directionToChair, moveDirection) < 0) |
| | | { |
| | | moveDirection = -moveDirection; |
| | | } |
| | | } |
| | | |
| | | public void OffDrag() |
| | |
| | | { |
| | | if (isDragging) |
| | | { |
| | | Vector3 newPosition = GetMouseWorldPos() + offset; |
| | | float deltaX = newPosition.x - startX; |
| | | float originalX = transform.localPosition.x; |
| | | // X 축으로만 이동하며, 최소/최대 범위 내에서만 이동 |
| | | float newX = Mathf.Clamp(originalX + deltaX * Sensitivity, minX, maxX); |
| | | Vector3 delta = Input.mousePosition - lastMousePosition; |
| | | float moveAmount = Vector3.Dot(delta, mainCamera.transform.right) * Sensitivity; |
| | | |
| | | // 미리 결정된 이동 방향을 사용 |
| | | float newX = transform.localPosition.x + moveDirection.x * moveAmount; |
| | | newX = Mathf.Clamp(newX, minX, maxX); |
| | | |
| | | transform.localPosition = new Vector3(newX, transform.localPosition.y, transform.localPosition.z); |
| | | |
| | | startX = transform.localPosition.x; |
| | | lastMousePosition = Input.mousePosition; |
| | | } |
| | | } |
| | | |
| | | Vector3 GetMouseWorldPos() |
| | | { |
| | | Vector3 mousePoint = Input.mousePosition; |
| | | mousePoint.z = mainCamera.WorldToScreenPoint(transform.localPosition).z; |
| | | return mainCamera.ScreenToWorldPoint(mousePoint); |
| | | } |
| | | } |