
Private bool IsRectTransformInsideSreen(RectTransform rectTransform)

/ This methods will check is the rect transform is inside the screen or not Public void OnEndDrag(PointerEventData eventData) / This method will be called at the end of mouse drag LastMousePosition = currentMousePosition Vector3 newPosition = rect.position + new Vector3(diff.x, diff.y, ) Vector2 diff = currentMousePosition - lastMousePosition Vector2 currentMousePosition = eventData.position Public void OnDrag(PointerEventData eventData) / This method will be called during the mouse drag Public void OnBeginDrag(PointerEventData eventData) / This method will be called on the start of the mouse drag Public class MouseDragBehaviour : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler Write your own functionality, if required. It also does not implement anything OnEndDrag method. Other UI elements are child of this panelīelow script also make sure that the panel will not go outside of the screen area.Panel Game Object is having Rect Transform.Step 2: Script to move canvas UI with mouse drag – drag and drop UI scriptĪttach below script to Panel Game Object. It can be any other option also, but do not make it stretch – stretch otherwise it will cover whole screen. The panel Anchor Presets is middle-center. The Canvas render mode is Screen Space – Overlay which is set by default.

Here, I have added a panel in the canvas and added couple of elements to it with some background. Create a new project in Unity and add some UI to this. Let’s set-up a simple project in Unity 3D for the this. Step by step guide for Drag and drop UIĭownload the unity package of below example – DragUnityPackage It will be like drag and drop UI from one position to another. we will see to move a canvas UI with mouse drag in Unity 3D. (This will happen surprisingly often.In this tutorial.

This is the recommended way to do things, because it makes your script more flexible: you won't have to rewrite it when there comes a time that the Rigidbody2D component needs to be attached to a different Game Object than your PlayerMovement.
Unity 3d movement script code#
Existing instances of the component whose GameObject lacks the new dependencies will not have those dependencies added automatically.Īlternatively, you can remove the code inside your Awake() method that fetches the component, and instead drag-and-drop the component in the inspector.

Note that RequireComponent only checks for missing dependencies during the moment the component is added to the GameObject. When you add a script which uses RequireComponent to a GameObject, the required component will automatically be added to the GameObject. View official documentation on RequireComponent. To save space, I didn't paste the rest of your code. Public class PlayerMovement : MonoBehaviour It goes at the top of the class definition, like so: // <- This is what I'm talking about. Since this script requires a Rigidbody2D to work, you can add a line that will automatically create it on the Game Object when your script is added. You have to attach a Rigidbody2D component to the Game Object that your script is sitting on. Most likely, body is null, because the component it's referencing has not been defined.
