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

Mouse coordinates are invalid and keeps going off charts

Discussion in 'Scripting' started by Xenomyth, Jul 31, 2019.

  1. Xenomyth

    Xenomyth

    Joined:
    Jul 31, 2019
    Posts:
    1
    Hello there, I'm trying to make a Drag and Drop system and I'm currently using this code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragDrop : MonoBehaviour {
    6.  
    7.     [SerializeField]
    8.     private Transform jamuContainer;
    9.     private Vector2 initialPosition;
    10.     private Vector3 mousePosition;
    11.     private float deltaX, deltaY;
    12.     public static bool locked;
    13.     public GameObject dragImage;
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         initialPosition = transform.position;
    19.         mousePosition = Input.mousePosition;
    20.         mousePosition.z = Camera.main.nearClipPlane;
    21.     }
    22.  
    23.     public void OnMouseDown() {
    24.         print ("Test");
    25.         if (!locked) {
    26.             dragImage.SetActive (true);
    27.             deltaX = Camera.main.ScreenToWorldPoint (mousePosition).x - dragImage.transform.position.x;
    28.             deltaY = Camera.main.ScreenToWorldPoint (mousePosition).y - dragImage.transform.position.y;
    29.         }
    30.     }
    31.  
    32.     public void OnMouseDrag() {
    33.         if (!locked) {
    34.             print ("TestDrag");
    35.             mousePosition = Camera.main.ScreenToWorldPoint (mousePosition);
    36.             dragImage.transform.position = new Vector2 (mousePosition.x - deltaX, mousePosition.y - deltaY);
    37.             print (mousePosition);
    38.             print (deltaX);
    39.             print (deltaY);
    40.             print (dragImage.transform.position.x);
    41.             print (dragImage.transform.position.y);
    42.         }
    43.     }
    44.  
    45.  
    46.     public void OnMouseUp() {
    47.         if (Mathf.Abs (transform.position.x - jamuContainer.position.x) <= 0.5f &&
    48.             Mathf.Abs (transform.position.y - jamuContainer.position.y) <= 0.5f) {
    49.             dragImage.SetActive (false);
    50.             //dragImage.transform.position = new Vector2(jamuContainer.position.x, jamuContainer.position.y);
    51.             locked = true;
    52.             print ("TestMouseUp");
    53.         } else {
    54.             dragImage.transform.position = new Vector2 (initialPosition.x, initialPosition.y);
    55.             dragImage.SetActive (false);
    56.         }
    57.     }
    I apply the codes on an Event Trigger of Event Types:
    • Pointer Down -> DragDrop.OnMouseDown();
    • Drag -> DragDrop.OnMouseDrag();
    • End Drag -> DragDrop.OnMouseUp();
    The end results are as attached in this thread. Basically the mouse position kept increasing in axis even though the actual pointer barely moved.

    Help is very much appreciated! Thank you.
     

    Attached Files: