Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Moving a 2D object with mouse - question about coordinates

Discussion in '2D' started by pml_machine, Aug 22, 2022.

  1. pml_machine

    pml_machine

    Joined:
    Jul 12, 2022
    Posts:
    4
    I'm a total beginner in Unity and I'm trying to understand things bit by bit. Wanting to move objects with my mouse I found a good tutorial. I have a script that works I just want to understand something.

    I just have a red circle object that I'm moving around, it has a 2d Collider component.

    The script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveIt : MonoBehaviour
    6.  
    7. {
    8.     public GameObject selectedObject;
    9.     Vector3 offset;
    10.     void Update()
    11.     {
    12.         Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    13.         if (Input.GetMouseButtonDown(0))
    14.         {
    15.             Collider2D targetObject = Physics2D.OverlapPoint(mousePosition);
    16.             if (targetObject)
    17.             {
    18.                 selectedObject = targetObject.transform.gameObject;
    19.                
    20.                 offset = selectedObject.transform.position - mousePosition;
    21.                 Debug.Log("Selected Object pos: " + selectedObject.transform.position + " Mouseposition: " + mousePosition + " Offset: " + offset);
    22.  
    23.             }
    24.         }
    25.         if (selectedObject)
    26.         {
    27.             selectedObject.transform.position = mousePosition + offset;
    28.         }
    29.         if (Input.GetMouseButtonUp(0) && selectedObject)
    30.         {
    31.             selectedObject = null;
    32.         }
    33.     }
    34. }
    35.  
    The object is moved to the mouse position and because the mouse position has a z component that is -10 this needs to be offset in transforming the position. I get that. However looking at the offset, being the difference selectedObject.transform.position - mousePosition there is also small variation in the x and y components. If I only click the red circle without moving the mouse the circle stays in the exact same spot, which is what I would want. However I don't understand why it doesn't move a little since the new position should be updated by
    selectedObject.transform.position = mousePosition + offset;
    . Shouldn't the circle snap to being centered around my mouse position?
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    If I understand correctly, it is currently not snapping due to the offset itself. Instead,
    selectedObject.transform.position = mousePosition;
    would be centered on the object. The offset tracks where the mouse was when you were able to click the collider, and you may have clicked to one side or the other of the object, so that preserved when you assign it back to the selectedObject.

    If you want it to be centered, then eliminate the offset (or only track the z). If your world doesn't have z levels (z is zero), then it's going to be easier and cleaner to eliminate offset and instead immediately zero the mousePosition:
    Code (CSharp):
    1. Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2. mousePosition.z = 0f;