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.

Resolved Dragging UI while moving not working right

Discussion in 'Scripting' started by EscherK, Nov 28, 2022.

  1. EscherK

    EscherK

    Joined:
    Jul 5, 2022
    Posts:
    45
    So, the canvas is in world space as a child of the camera.
    The problem is that if I move or rotate the camera the UI goes all over the place, like this:


    This is the code as of yet, I'm not sure how to fix it:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class Draggable : MonoBehaviour, IDragHandler
    5. {
    6.     public Camera cam;
    7.  
    8.     public void OnDrag(PointerEventData data)
    9.     {
    10.         Vector3 mouseWorld = cam.ScreenToWorldPoint(new Vector3(MouseObject.screenPos.x, MouseObject.screenPos.y, transform.position.z));
    11.         mouseWorld.z = transform.position.z;
    12.         transform.position = mouseWorld;
    13.     }
    14. }
    15.  
    Thanks in advance
     
    Last edited: Nov 28, 2022
  2. EscherK

    EscherK

    Joined:
    Jul 5, 2022
    Posts:
    45
    Solved it, the z part of the cam.ScreenToWorldPoint has to be a fixed number, as it is the distance from the camera
     
    Last edited: Nov 29, 2022