Drag & Drop 3D | World-Space 3D Objects | C# | Unity Game Engine

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 35

  • @jeandiegoortegamendoza3894
    @jeandiegoortegamendoza3894 10 місяців тому +1

    Estaba buscando estoy desde hace 3 dias y por fin logre encontrar un tutorial simple, muchas gracias

  • @tahacap8573
    @tahacap8573 2 роки тому +1

    Amazing. Clean and short. #respect

  • @miloszlatanovic1686
    @miloszlatanovic1686 11 місяців тому

    Excellent tutorial.
    A little suggestion due to optimization
    Caching camera.main .

  • @amirmanzoor8728
    @amirmanzoor8728 2 роки тому +1

    this is the awesome tutorial
    , thank u for sharing sir🥰

  • @austinwilliamsshow
    @austinwilliamsshow Рік тому +1

    This is epic! Thank you so much!

  • @Alafandri
    @Alafandri Рік тому +1

    how do I add rigidbody? Its not working for me with rigidbody on...

  • @dragonarc_gaming
    @dragonarc_gaming Рік тому +1

    I can't seem to make this work on the 2D. My changes on the code is changing the GetComponent into GetComponent
    I use circle and box as the sprite. The box is given the tag "DropArea". The circle can't seem to stick into the box. Hope anyone can help

    • @yoruryoshi2288
      @yoruryoshi2288 Рік тому +1

      im having the exact same problem

    • @Unity3DSchool
      @Unity3DSchool  Рік тому

      My apologies for the late reply and thanks for reporting this.
      Some more changes are needed for this to work with 2D. So I will make a separate video for this. Meanwhile, you can make this work by following this-
      1. Make sure you replace the Collider with Collider2D everywhere.
      2. Update the OnMouseDown method to support 2D raycasting-
      *********************************
      void OnMouseUp()
      {
      var rayOrigin = Camera.main.transform.position;
      var rayDirection = MouseWorldPosition() - Camera.main.transform.position;
      RaycastHit2D hitInfo;
      if (hitInfo = Physics2D.Raycast(rayOrigin, rayDirection))
      {
      if (hitInfo.transform.tag == destinationTag)
      {
      transform.position = hitInfo.transform.position + new Vector3(0, 0, -0.01f);
      }
      }
      transform.GetComponent().enabled = true;
      }
      *************************************************

    • @SunnyMeditation
      @SunnyMeditation Рік тому

      i follow the code but i can't did it....may i know how to change this drag and drop function from world space 3d in 2d objects? i need to change my draggable object and drop area both ui 2d image...i try many times to modify it but fail..@@Unity3DSchool

    • @SunnyMeditation
      @SunnyMeditation Рік тому

      hHere is my updated code...
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class DragDrop : MonoBehaviour
      {
      Vector3 offset;
      public string destinationTag = "DropArea";
      void OnMouseDown()
      {
      offset = transform.position - MouseWorldPosition();
      transform.GetComponent().enabled = false;
      }
      void OnMouseDrag()
      {
      transform.position = MouseWorldPosition() + offset;
      }
      void OnMouseUp()
      {
      var rayOrigin = Camera.main.transform.position;
      var rayDirection = MouseWorldPosition() - Camera.main.transform.position;
      RaycastHit2D hitInfo;
      if (hitInfo = Physics2D.Raycast(rayOrigin, rayDirection))
      {
      if (hitInfo.transform.tag == destinationTag)
      {
      transform.position = hitInfo.transform.position + new Vector3(0, 0, -0.01f);
      }
      }
      transform.GetComponent().enabled = true;
      }
      Vector3 MouseWorldPosition()
      {
      var mouseScreenPos = Input.mousePosition;
      mouseScreenPos.z = Camera.main.WorldToScreenPoint(transform.position).z;
      return Camera.main.ScreenToWorldPoint(mouseScreenPos);
      }
      }@@Unity3DSchool

  • @SunnyMeditation
    @SunnyMeditation Рік тому

    sorry,but may i know how to change this world space 3d in 2d objects?become my draggable object and drop are both is 2d image...i try many times to modify it but fail..

  • @bonbongaming6995
    @bonbongaming6995 Рік тому

    Hello @unity3dschool, can I know how to expand the snapping range?

  • @amandam992
    @amandam992 2 роки тому +1

    finally something that works

  • @raphm8384
    @raphm8384 2 роки тому

    Can only move object on 2 axis, any suggestions to make this work on all 3 axis?

  • @cassidynoble5466
    @cassidynoble5466 3 роки тому +1

    Thank you so much!

  • @maryamsari5045
    @maryamsari5045 9 місяців тому

    Thanks for this Amazing tutorial, may I ask how can I make it works for TouchPhase? I've tried some ways, but no success!

  • @3venthoriz0n
    @3venthoriz0n 2 роки тому

    Is it possible to snap the object to a route/path/vector/spline?

  • @onoffonist
    @onoffonist 2 роки тому

    Exactly what i was looking for!
    But i have one question, why do you remove the start() and update() methods?

  • @jaimetheone9150
    @jaimetheone9150 8 місяців тому

    I finally made it work, my problem was with the damn camera tag.
    I was getting the error: "NullReferenceException: Object reference not set to an instance of an object".
    After reading what Camera.main does in Visual Studio, it says it connects to a camera tagged as "MainCamera". So I went to my camera and the tag was already there, I selected it and it worked!

  • @doanxuanthang6545
    @doanxuanthang6545 2 роки тому +1

    Hey, thank you for the video , Can it be used on mobile devices?

  • @HassanGanari
    @HassanGanari 3 роки тому +1

    Hey, thank you for the tutorial, really easy to follow.
    One question : how can i make the object unselectable after it snaps to its destination ? thx
    liked and subscribed.

    • @Unity3DSchool
      @Unity3DSchool  3 роки тому +1

      Don't enable collider if it snaps to destination.

    • @coffeebean7502
      @coffeebean7502 2 роки тому

      @@Unity3DSchool I can't figure out how to do it please help.

    • @coffeebean7502
      @coffeebean7502 2 роки тому

      ​@@Unity3DSchool I've figured it out, now the problem is, I need to disable the destination object's collider so if one object snaps to it, it won't allow any other objects to snap on it again. This is the one that I can't figure out.

    • @danielewusi-essel219
      @danielewusi-essel219 2 роки тому

      @@coffeebean7502 you can try something like this....
      private void OnTriggerEnter2D(Collider2D collision)
      {
      if (collision.gameObject.name== "Target")
      { collision.gameObject.transform.GetComponent().enabled=false;
      }
      // this will disable the target's collider
      // this is for 2D games, you can change it to 3D if you wish
      // let me know if this works for you

  • @felaix5885
    @felaix5885 3 роки тому

    hey, thanks for the GREAT tutorial. i get the error "Assets\Scripts\DragDrop.cs(38,57): error CS1615: Argument 3 may not be passed with the 'out' keyword". can u help me?

    • @Unity3DSchool
      @Unity3DSchool  3 роки тому

      On line 38, the 3rd parameter of method is using out keyword. I am guessing that Physics.Raycast method is present on line 38 and 3rd parameter is using 'out' but this parameter is not RaycastHit type. Maybe you are using layerMask or maxDistance as third parameter and using 'out' with that which is wrong.
      In Physics.Raycast method, only use 'out' with RaycastHit type parameter.

  • @theauditor322
    @theauditor322 2 роки тому

    Hello there! Amazing tutorial, may I ask how can you make a code for like a game over screen or a "you win" screen after all the pieces are in place?

    • @Unity3DSchool
      @Unity3DSchool  2 роки тому

      One way is to use a counter with starting value 0. Everytime a piece is placed at correct position, increment the counter. Once counter == place count, Show win msg.

    • @SunnyMeditation
      @SunnyMeditation Рік тому

      Can I have the complete script?Thanks!
      @@Unity3DSchool

  • @akmalhusainiismail8933
    @akmalhusainiismail8933 2 роки тому

    Hi, i try to follow your tutorial and receive an error : The name 'destinationTag' does not exist in the current context. Is this because of the tag on the object ?

  • @sivacse24
    @sivacse24 3 роки тому

    Code is clean and simple... Cool tutorial
    var pos = Input.mousePosition;
    pos.z = _cam.WorldToScreenPoint(Input.mousePosition).z;
    return _cam.ScreenToWorldPoint(pos);
    One thing want to know _cam.WorldToScreenPoint(Input.mousePosition).z why only z axis is used here?

    • @Unity3DSchool
      @Unity3DSchool  3 роки тому +3

      Input.mousePosition gives the mouse position on screen and screen is 2D so z will be 0 in vector3 pos. Now we need correct z value for picked object. For this you have to write -> pos.z = _cam.WorldToScreenPoint(*YourObjectWorldPosition*).z; Passing input.mousePosition here will not work.
      Hope it helps.