Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Reset position on drag and drop script

Discussion in 'Getting Started' started by AkatoshIR, May 2, 2023.

  1. AkatoshIR

    AkatoshIR

    Joined:
    Oct 20, 2021
    Posts:
    1
    im trying to make darg and drop script to move things in game but the thing this is for mobile devise. i want to player not be able to drop item in place that should not be placed so i want to reset it position to startPostion. the problem is in OnMouseUp() but i will send all of script. here is the code that i write and i have problem with it:
    Code (CSharp):
    1. using UnityEngine;
    2. [RequireComponent(typeof(Collider2D))] public class DragAndDrop : MonoBehaviour {
    3. private bool isDragging;
    4. private Plane dragPlane;
    5. private Vector3 offset;
    6. private Camera myMainCamera;
    7. private Collider2D myCollider;
    8. private Vector3 startPosition;
    9. void Start() {
    10.      myMainCamera = Camera.main;
    11.      myCollider = GetComponent<Collider2D>();
    12.      startPosition = transform.position;
    13. }
    14. void OnMouseDown() {
    15.      startPosition = transform.position;
    16.      isDragging = true;
    17.      myCollider.isTrigger = true;
    18.      dragPlane = new Plane(myMainCamera.transform.forward, transform.position);
    19.      Ray camRay = myMainCamera.ScreenPointToRay(Input.mousePosition);
    20.      float planeDist;
    21.      if (dragPlane.Raycast(camRay, out planeDist)) {
    22.          offset = transform.position - camRay.GetPoint(planeDist);
    23.      }
    24. }
    25. void OnMouseDrag() {
    26.      Ray camRay = myMainCamera.ScreenPointToRay(Input.mousePosition);
    27.      float planeDist;
    28.      if (dragPlane.Raycast(camRay, out planeDist)) {
    29.          Vector3 newPosition = camRay.GetPoint(planeDist) + offset;
    30.          // Check if the new position is within the bounds of the object,
    31.          // with some extra padding added to the edges
    32.          Vector3 screenPoint = myMainCamera.WorldToScreenPoint(newPosition);
    33.          Vector3 objectBounds = GetComponent<Renderer>().bounds.extents;
    34.          float paddingX = objectBounds.x * 0.5f; // Add 25% extra padding on each side
    35.          float paddingY = objectBounds.y * 0.5f; // Add 25% extra padding on each side
    36.          if (screenPoint.x > objectBounds.x - paddingX && screenPoint.x < Screen.width - objectBounds.x + paddingX
    37.                                                        && screenPoint.y > objectBounds.y - paddingY && screenPoint.y < Screen.height - objectBounds.y + paddingY) {
    38.              transform.position = newPosition;
    39.          }
    40.      }
    41. }
    42. // void OnMouseUp() {
    43. //     isDragging = false;
    44. //     myCollider.isTrigger = false;
    45. // }
    46. void OnMouseUp() {
    47.      isDragging = false;
    48.      myCollider.isTrigger = false;
    49.      // Check if there is any collider under the item's position
    50.      Collider2D hitCollider = Physics2D.OverlapBox(transform.position, myCollider.bounds.size, 0);
    51.      if (hitCollider != null ) {
    52.          // There is a collider, so check if it is the same as the item's collider
    53.          if (hitCollider != myCollider || hitCollider.CompareTag("Ground")) {
    54.              // The item was dropped on a different collider, so reset its position
    55.              transform.position = startPosition;
    56.          }
    57.      }
    58. }
    59. }
    i find overlapBox in unity document but it just not work properly . i also forgot to mention its 2d Game and this script attached to 2d sprite in game.

    i also tried OverlapCircleAll method but that not working optimal as well