playMaker

Author Topic: Drag in World Space - Rect Transform Screen Point to World Point in Rectangle  (Read 719 times)

RPM37

  • Playmaker Newbie
  • *
  • Posts: 2
Hi Everybody,

I learned about this action from a previous post from 6 years ago. I'm currently trying to set up a drag and drop FSM for a 2d object on a canvas in world space render mode. My initial attempt some what works by using UI Get Last Pointer Data Info and Translate Advance to have the object follow the mouse pointer but the object tends to lag behind the mouse pointer while dragging. I'm thinking this is because the canvas is in world space mode and is not translating the on screen position of the mouse pointer to the world position of the 2d object on the canvas.

Basically, I'm not sure how exactly I need to use Rect Transform Screen Point to World Point in Rectangle action to get this to work right. Currently my drag and drop FSMs are on the 2D objects themselves. Is this correct or do I need to have an FSM for drag on the canvas as well? I'll post some screen shots to help further clarify.   

Also, just so you have an understanding of the project, I'm making a trading card game using Unity UI Canvas in world space render mode. The canvas itself is placed over top a 3d game board. 

Tubbritt

  • Playmaker Newbie
  • *
  • Posts: 8
Hello.
This will be an odd reply, but here goes, I’m in the middle of some R&D work right now testing an AI, and I decided to throw your question at it.

Funny enough, the reply it gave actually sounds like it could be the answer to your problem.

Quote
To make the drag and drop FSM work correctly for a 2D object on a canvas in world space render mode, you need to use the Rect Transform Screen Point to World Point in Rectangle action correctly. This action will convert the screen point of the mouse pointer to the world position of the 2D object on the canvas.

You should have the drag and drop FSM on the 2D object itself, rather than on the canvas. This is because you want the object to follow the mouse pointer, and the object is the one that needs to be moved.

Here's an example of how you can set up the FSM:

    Create a new FSM on your 2D object and add the following states: Idle, Dragging, and Drop.

    In the Idle state, add a Mouse Down event and transition to the Dragging state.

    In the Dragging state, add the UI Get Last Pointer Data Info action to get the position of the mouse pointer.

    Add the Rect Transform Screen Point to World Point in Rectangle action to convert the screen point to the world position of the object on the canvas. Use the RectTransform parameter to specify the RectTransform of the canvas.

    Add the Translate action to move the object to the new position. Use the Vector parameter to specify the new position.

    Add a Mouse Up event and transition to the Drop state.

    In the Drop state, add any actions you want to perform when the object is dropped, and transition back to the Idle state.

Make sure you set the Interactable property of the Canvas Group component on the canvas to true, so that the canvas can receive mouse events.

By using the Rect Transform Screen Point to World Point in Rectangle action correctly, you should be able to make the object follow the mouse pointer without lagging behind.

I asked it to verify this, because it sounded right but I wasn't sure, and it said the following.

Quote
I apologize if my previous response was not helpful or if there was any misunderstanding. As an AI language model, I try my best to provide accurate and helpful answers to the best of my knowledge and capabilities based on the information provided.

To clarify, if you are trying to implement drag and drop functionality for a 2D object on a canvas in world space render mode using Playmaker, you can use the following steps:

    Create a new FSM on the 2D object you want to drag and drop.

    In the FSM, add a state called "Idle" and set it as the start state.

    Add an event trigger for "Pointer Down" and transition to a new state called "Dragging".

    In the "Dragging" state, add the following actions:

    "UI Get Last Pointer Data Info" action to get the position of the mouse pointer
    "Rect Transform Screen Point to World Point in Rectangle" action to convert the screen point to the world position of the object on the canvas. Specify the RectTransform parameter as the RectTransform of the canvas.
    "Translate" action to move the object to the new position. Specify the Vector parameter as the converted world position.

    Add an event trigger for "Pointer Up" and transition to a new state called "Dropping".

    In the "Dropping" state, add any additional actions you want to perform when the object is dropped, and transition back to the "Idle" state.

Note: It's important to make sure the Canvas Group component of the canvas is set to "Interactable" and "Blocks Raycasts" to enable mouse input for the canvas.

I hope this helps! Let me know if you have any further questions or if there's anything else I can assist you with.

Anwyay... I hope this helps you or at least points you in the right direction.