ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

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

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 พ.ย. 2021
  • Copy code from here-
    u3ds.blogspot....
    Feel free to Like and Share to show support for this channel.
    Don't forget to leave a comment if anything comes to mind.
    Have a nice day :)
    #unity #csharp #tutorial

ความคิดเห็น • 36

  • @jeandiegoortegamendoza3894
    @jeandiegoortegamendoza3894 4 หลายเดือนก่อน +1

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

  • @miloszlatanovic1686
    @miloszlatanovic1686 5 หลายเดือนก่อน

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

  • @tahacap8573
    @tahacap8573 ปีที่แล้ว +1

    Amazing. Clean and short. #respect

  • @amirmanzoor8728
    @amirmanzoor8728 2 ปีที่แล้ว +1

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

  • @austinwilliamsshow
    @austinwilliamsshow ปีที่แล้ว +1

    This is epic! Thank you so much!

  • @amandam992
    @amandam992 2 ปีที่แล้ว +1

    finally something that works

  • @jaimetheone9150
    @jaimetheone9150 3 หลายเดือนก่อน

    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!

  • @maryamsari5045
    @maryamsari5045 3 หลายเดือนก่อน

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

  • @cassidynoble5466
    @cassidynoble5466 2 ปีที่แล้ว +1

    Thank you so much!

  • @doanxuanthang6545
    @doanxuanthang6545 ปีที่แล้ว +1

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

  • @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;
      }
      *************************************************

    • @bloomingyouth5736
      @bloomingyouth5736 8 หลายเดือนก่อน

      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

    • @bloomingyouth5736
      @bloomingyouth5736 8 หลายเดือนก่อน

      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

  • @Alafandri
    @Alafandri ปีที่แล้ว +1

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

  • @bloomingyouth5736
    @bloomingyouth5736 8 หลายเดือนก่อน

    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 6 หลายเดือนก่อน

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

  • @onoffonist
    @onoffonist 2 ปีที่แล้ว

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

    • @wegneren9672
      @wegneren9672 ปีที่แล้ว +1

      Since the start and update method is not used, always remove code that is not being used :)

  • @HassanGanari
    @HassanGanari 2 ปีที่แล้ว +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  2 ปีที่แล้ว +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

  • @raphm8384
    @raphm8384 2 ปีที่แล้ว

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

  • @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.

    • @bloomingyouth5736
      @bloomingyouth5736 7 หลายเดือนก่อน

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

  • @3venthoriz0n
    @3venthoriz0n 2 ปีที่แล้ว

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

  • @felaix5885
    @felaix5885 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      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.

  • @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 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +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.