ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
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
Estaba buscando estoy desde hace 3 dias y por fin logre encontrar un tutorial simple, muchas gracias
Excellent tutorial.
A little suggestion due to optimization
Caching camera.main .
Amazing. Clean and short. #respect
this is the awesome tutorial
, thank u for sharing sir🥰
This is epic! Thank you so much!
finally something that works
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!
Thanks for this Amazing tutorial, may I ask how can I make it works for TouchPhase? I've tried some ways, but no success!
Thank you so much!
Hey, thank you for the video , Can it be used on mobile devices?
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
im having the exact same problem
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;
}
*************************************************
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
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
how do I add rigidbody? Its not working for me with rigidbody on...
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..
Hello @unity3dschool, can I know how to expand the snapping range?
Exactly what i was looking for!
But i have one question, why do you remove the start() and update() methods?
Since the start and update method is not used, always remove code that is not being used :)
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.
Don't enable collider if it snaps to destination.
@@Unity3DSchool I can't figure out how to do it please help.
@@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.
@@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
Can only move object on 2 axis, any suggestions to make this work on all 3 axis?
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?
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.
Can I have the complete script?Thanks!
@@Unity3DSchool
Is it possible to snap the object to a route/path/vector/spline?
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?
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.
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 ?
My bad i miss one line of your script
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?
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.