For those having issues make sure your camera for the scene is tagged as being "main camera", not as "untagged", the option for this is just under the name for the camera in the inspector panel.
If anyone wants to stop movement along a particular axis, the easy way is to go into the rigidbody component of the object you're moving/dragging, and under constraints, "Freeze" the axis you don't want to use. :D
Thank you for the tutorial, but there is a small mistake in your script. If you pick your object up and let it loose, it lands correctly, in the same spot where you have picked it up, if your camera is NOT rotated, only. If you rotate your camera downwards, the script will do its job as it should but with the rigidbody attached, the object will fall straight down to the ground + the "rotationOffset" of the camera. For example: Your camera rotation is (20, 0, 0), your objectsPosition is (-10, 1, 0). You lift it up to "y = 9" and let go. You should notice, that the new coordninate is something like (-10.6, 9, 2.9) and not (-10, 1, 0), because it adds the rotation of the camera to it. The same problem occurs for cameraRotations in y and z.
Thank You!, I've been using this to make my fps game, at first i thought it was very hard to do, but then i realized just how easy it is only a few lines of code. Thank You!
with this method, the calculated world position will have a fixed z value and the object will only move along x/y axis cuz you're using the clip space z value of the object to construct the vec3, but is there a way to make it have a fixed y value and the object move along x/z axis?
@@jn1703 I solved by creating a plane and calculate the intersection between the play and the ray casted to the mouse position. E.g. if you want to use mouse to place an object on the ground, you can create a plane of normal vec3(0, 1, 0), then cast a ray from eye position to mouse, the intersection point would be the position of the object
@@slmjkdbtl Thanks for your response but not sure I'm following, I have my camera rotated at 45 degrees and just want to move the object on these axis. How can I get the correct mouse offset to work with my objects current postiion?
having an issue where when I drag the object... it just goes flying off screen my camera has to move around the world and I would like ti to be able to go from any position and pick up and drack the object into specific places... by when I run the code, it just seems to make the objects fly of into the void. anyone got any way to fix this? edit: so i simply copied the code and its seems to work now (slightly stange), but still face an issue with my camera moving with the object... it seems that the objects gravity is accelerated when i move my camera around edit2: also figured that out... have the rigidbody be set to zero when dragging
@@createria2 that'll slow down the gravity of the entire game i suggest turning off Use Gravity in your draggable objects rigidbody when Mouse is down and turning it back one with the mouse button is up Good luck! @Jakub Majewski
unity is throwing up errors like 'Assets\Drag.cs(14,19): error CS0120: An object reference is required for the non-static field, method, or property 'GameObject.transform' and Assets\Drag.cs(24,24): error CS0103: The name 'GetMousePosWorld' does not exist in the current context. please help
Two problems/Questions. First, it's very sensitive. Is there a way to slow down the movement? Second, how can i lock the Y axis? I tried the obvious thing of freezing Y axis on its rigidbody but that didnt work.
Hi, thanks for the great tutorial. But I'm having "Assets/DragFox.cs(23,23): Error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3' to 'float' (CS0029)". It seems that mZCoord can't be float?
How could I go about making the object stop once it hits a wall whilst dragging? Currently it goes through anything but if I collide with something it should just stop
Maybe use the hinge-method that unityengine provides? Shouldn't be too hard creating a gameobject at the mouse-position connecting it to your object at the raycast-point where the mouse clicks. That will probably create the physics-effect you want :)
hmm why cant i "clamp" the y position instead of z? if i change the values simply to y, it just do not react when i try to drag it. but when i change it back to Z it works again :/
Nice! I want to be able to pick up an object and have it "snap" to the center (position and rotation) so I can carry it around and drop it somewhere else in a first person game. What's the best way to do that?
Jayanam Yeah, exactly! I wrote a simple script that lets me pick up objects and carry them around, but if they hit a wall they get off center. I also want the objects to rotate so they face the camera properly.
I am having a little issue using this in on a boardlike scene. Moving the Object towards the player will cause it to move down and (potentionally) into the ground. Any soloutions?
Can you tell me how to make the object by clicking the mouse become a little to the left or to the right, and releasing the mouse returned to the center as it was before clicking?(for kinematic)
4 года назад
I need this for Super Mario 64 remake to stretch Mario head Bur, this script works on unity 2017?
nice tutorial thank you but i have a problem. when i dragged an object to a wall , it passes through the wall. i think i have to add velocity or something like that for dragging. but i dont know how to do it :/
My object disappears from the game window when I click on it. I logged the object coordinates in the console and it seems to be sending it way off screen. Unity 2019.2.18f1
I applied this to an object in my scene, but it just gives me a ton of errors whenever I click on the object I applied it to. They all say “Object reference not set to an instance of an object”
For those having issues make sure your camera for the scene is tagged as being "main camera", not as "untagged", the option for this is just under the name for the camera in the inspector panel.
thanks!!!
Thanks so Much!
Thanks. It works but I am not sure why
This is exceptional. Clear, fast, clean and massively useful. I'm now prompted to look at a lot more by the author.
Thanks for this, this was super clear and easy to follow!
Omg, You star, you managed to solve my problem, thank you a million times over.!!
Just a little adaptation to fulfill my implementation and its working like a charm. Thanks a lot!!
really cool! many thanks!
Glad you liked it!
OMG I finally found something that works! Thank you!
Thank you very much! I spent many hours tryng to do this but it didn't work, but this video did!! I'm very happy!
Quick & easy to understand. Thank you very much.
Great explanation, quick and simple to implement.
If anyone wants to stop movement along a particular axis, the easy way is to go into the rigidbody component of the object you're moving/dragging, and under constraints, "Freeze" the axis you don't want to use. :D
Well done! It helped me to understand the concept in practice were now I can tailor it for my requirements. Thanks
Simple and working amazing still in 2022 :)
how to fix the objects clipping through the ground? while you are dragging it
Thank you for the tutorial, but there is a small mistake in your script.
If you pick your object up and let it loose, it lands correctly, in the same spot where you have picked it up, if your camera is NOT rotated, only. If you rotate your camera downwards, the script will do its job as it should but with the rigidbody attached, the object will fall straight down to the ground + the "rotationOffset" of the camera.
For example: Your camera rotation is (20, 0, 0), your objectsPosition is (-10, 1, 0). You lift it up to "y = 9" and let go. You should notice, that the new coordninate is something like (-10.6, 9, 2.9) and not (-10, 1, 0), because it adds the rotation of the camera to it.
The same problem occurs for cameraRotations in y and z.
PERFECT NOW I CAN MAKE AN amnesia like door!
Thank You!, I've been using this to make my fps game, at first i thought it was very hard to do, but then i realized just how easy it is only a few lines of code. Thank You!
THANK YOU, you are amazing
Very nice and clear tutorial ! Thanks !
Thanks For Help
But I want to know How to move 3D object by touch dragging Finger By the screen means our object should follow our finger dirction
Thanks! You really Helped Me For My Game!
Glad I could help!
@@JayAnAm :)
Tutorial is too quick, but after watching it several times i got it.
I don’t understand shit since i am a beginner. But it works like a charm
Thanks buddy really really Appricaiated
excellent tutorial, thanks for this !
Thx, you're welcome
with this method, the calculated world position will have a fixed z value and the object will only move along x/y axis cuz you're using the clip space z value of the object to construct the vec3, but is there a way to make it have a fixed y value and the object move along x/z axis?
I'd also very much like to know this
@@jn1703 I solved by creating a plane and calculate the intersection between the play and the ray casted to the mouse position. E.g. if you want to use mouse to place an object on the ground, you can create a plane of normal vec3(0, 1, 0), then cast a ray from eye position to mouse, the intersection point would be the position of the object
@@slmjkdbtl Thanks for your response but not sure I'm following, I have my camera rotated at 45 degrees and just want to move the object on these axis. How can I get the correct mouse offset to work with my objects current postiion?
Works as magic, thank you!
having an issue where when I drag the object... it just goes flying off screen
my camera has to move around the world and I would like ti to be able to go from any position and pick up and drack the object into specific places... by when I run the code, it just seems to make the objects fly of into the void. anyone got any way to fix this?
edit: so i simply copied the code and its seems to work now (slightly stange), but still face an issue with my camera moving with the object... it seems that the objects gravity is accelerated when i move my camera around
edit2: also figured that out... have the rigidbody be set to zero when dragging
Excelent Tutorial!!! thnx so much, that helps me alot!!! Regars from Argentina!!!
Thanks a lot. Helped me a lot.
i like it but the velocity keeps going up as the object is in the air, any way to make the velocity more realistic? everything goes through the ground
add more gravity
go to edit: project settings and add a (-0) value or a lower walue (+) means less (-) means
@@createria2 that'll slow down the gravity of the entire game i suggest turning off Use Gravity in your draggable objects rigidbody when Mouse is down and turning it back one with the mouse button is up
Good luck! @Jakub Majewski
@@DeezNuts-hy7qn Thanks!! Really helped me!
@@acompletelynormalrobloxgam7383 no problem
Nice, thx to allow us easy copy past
unity is throwing up errors like 'Assets\Drag.cs(14,19): error CS0120: An object reference is required for the non-static field, method, or property 'GameObject.transform' and Assets\Drag.cs(24,24): error CS0103: The name 'GetMousePosWorld' does not exist in the current context. please help
For the second error check if your method name 'GetMousePosWorld' is exactly the same as where you are calling the method in 'OnMouseDown'.
Fantastic tutorial!!!
thankyou very nice , it helped me alot.
Best tutorial ever
Object can be dragged thru other objects floor etc, How 2 fix?
Also looking for a fix
u find a fix yet?
@@TheDynamicDudes Add a 2D collider to the cube and floor, etc... and add a ground/collision check. Check out brackey's tutorial on that.
need to incorporate collider components
and onCollision events
@@ryana.9821 incorporate is a cooking word
with the rock its bout drive its bout powa
lifesaver Thanks man. God Bless You.
Glad it helped
@@JayAnAm Hey, do you have any idea what trs is at 1:57 my game isn't accepting it and I'm using 2018.4.
Two problems/Questions. First, it's very sensitive. Is there a way to slow down the movement?
Second, how can i lock the Y axis? I tried the obvious thing of freezing Y axis on its rigidbody but that didnt work.
did you find a way to lock it? i need it too
multiply the position by a float that is named smooth you can adjust it to your liking im not sure if this works but it should work
Good Luck!
Will this be compatible with mobile devices or touchscreens?
Thank you
How can I freeze the y-axis while dragging whis this base code?
How can I lock its movement to only the X axis and also stop when touching another collider (such as ground)?
İf when ı want to for mobile touch how ı can doit?
Hi, thanks for the great tutorial. But I'm having "Assets/DragFox.cs(23,23): Error CS0029: Cannot implicitly convert type 'UnityEngine.Vector3' to 'float' (CS0029)". It seems that mZCoord can't be float?
apologies, I jst missed .z coordinate in this expression mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Hey man...I love this
I just wanna..know...how do I move the object by dragging along the path...
...help.plzzzz
It works! Thank you!
Is there any way how to move object only in two axis at once? Example: left mouse button move in X and Y and right mouse button move in Z and Y?
Not working for me. Also no errors given... I use a cube with nothing else (no rigid body, throewable, etc)
So you didnt add a rigidbody? Because you will need one 03:03
@@JayAnAm hi. I tried at first with one
How could I go about making the object stop once it hits a wall whilst dragging? Currently it goes through anything but if I collide with something it should just stop
Hello I'm having same issue. If you have found any solution then please help me 🙏
Apparently Callback Executor needs to derive from Mono Behavior??? Help!
Thank you so much
Do you happen to have a similar tutorial for touch input?
physics while dragging? like it flops around when i drag it
Maybe use the hinge-method that unityengine provides? Shouldn't be too hard creating a gameobject at the mouse-position connecting it to your object at the raycast-point where the mouse clicks. That will probably create the physics-effect you want :)
@@saiverx You can also set `isKinematic` to `true` in `OnMouseDown` and reset it back to `false` on `OnMouseUp`.
@@naterivard It will flop on gravity, but mouse movement itself will not create any force
hmm why cant i "clamp" the y position instead of z?
if i change the values simply to y, it just do not react when i try to drag it. but when i change it back to Z it works again :/
I have a same problem It's driving me crazy please help me I'm crying My tears are making a lake
@@김채윤-i8z uff sorry its already too long ago.
i think i just keep searching for other solutions or i tried around with that one.. i dont remember
Does anyone know how to fix that the game objects can be dragged through walls and floors?
Does this work for mobile(IPhone). I know it gets mouse input but i heard that mouse input also works with touch controls is that correct?
Yes That is correct
Nice solution, thanks bro :)
Do you know how to lock it so it doesn't move the object in the y axis?
estou com a mesma duvida
I want the same too, lock axis
How would i limit the mouse to a specific area?
Remixx i have completely given up on that lmao
why the movement speed is so slow?
what we do if we need to drag object on only x axis
Nice! I want to be able to pick up an object and have it "snap" to the center (position and rotation) so I can carry it around and drop it somewhere else in a first person game. What's the best way to do that?
You want to snap it to the hand of the first person player?
Jayanam Yeah, exactly! I wrote a simple script that lets me pick up objects and carry them around, but if they hit a wall they get off center. I also want the objects to rotate so they face the camera properly.
I can get it to work but only if the objects are closer to the camera??? How can I get it to select distant objects (not that distant really)
Check Here ruclips.net/video/FKEXWn68DSA/видео.html
Can somebody please tell me how to use the script on a prefab? My script is not working on the prefab.
I am very very new to Unity and need help.
Thank you so much man
Anybody Knows how to add like a white drag line?
Thanks bro ! can you help pls i need delete Z and Y just move object on X
You need to ad a rigidbody then go on constraint and freeze the position you dont want it to go on or rotation
@@thepotatogaming2340 nope. that doesn't work, because the script ignores that.
Thanks my dude!
Isn't this available in the editor already?
I am having a little issue using this in on a boardlike scene. Moving the Object towards the player will cause it to move down and (potentionally) into the ground. Any soloutions?
i'm using Unity 2020.1.10f1 and for some reason in visual studio code i got an error code C1513 } expected
you forgot a closing bracket
thanks but the object is rotating
Constraint the rotation in the rigidbody setting
How do you drag one object by clicking anywhere?
Can you tell me how to make the object by clicking the mouse become a little to the left or to the right, and releasing the mouse returned to the center as it was before clicking?(for kinematic)
I need this for Super Mario 64 remake to stretch Mario head
Bur, this script works on unity 2017?
Cool, It actually works.
nice tutorial thank you but i have a problem. when i dragged an object to a wall , it passes through the wall. i think i have to add velocity or something like that for dragging. but i dont know how to do it :/
I want the same too
VERY SIMPLE ADD THIS "mousePoint.y = 0;" TO GetMouseWorldPos() function
this way you lock the y axis
Short and sweet. Really nice video :)
thx 🙂
How to do this with 2D object
works the same, if your using sprites - only use x and y and within the context of Vector2 instead of Vector3
Here script who want to lock gameobject by Y axis - pastebin.com/AV0cgtnu
You can lock any axis you want
Btw, you know you dont need to write gameObject.transform , just transform is fine, or this.transform
:-) Yes, thx. But I found it a good practice for demonstrating that the gameobject has a transform.
Thank you good man!
perfect! thanks!
Translate please, почему одного ScreenToWorldPoint недостаточно для перемещения объекта, для чего нужен moffset, что это такое?
Good job! !
Thank You.
Thx, you're welcome
RUclips is broken. 3 advertisements for 3 and bit minutes of video.
little question: how would you limit the object from being able to get dragged through walls?
either raycasts or box colliders on your walls
Thx
My object disappears from the game window when I click on it. I logged the object coordinates in the console and it seems to be sending it way off screen. Unity 2019.2.18f1
same
nwm I copied the script from the desc and it worked
so this script works fine for me!!!
but can someone explain me what's happening?
well, turn on the sound :D
does this version work with visual studio code?
Hey how can i move that only in x axis?
I applied this to an object in my scene, but it just gives me a ton of errors whenever I click on the object I applied it to. They all say “Object reference not set to an instance of an object”
Seems like you didnt assign the drag object in the script (inspector)
Jayanam
But I did everything you did in the video. There is no options in the script to change anything in the inspector.
@@Laxhoop Yes, my bad... perhaps you dont have a main camera in your scene?
Jayanam
I do.
@@Laxhoop At which line does the error occur? Are you familiar with attaching a debugger?
THE SCRIPT boyz
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dragobiject : MonoBehaviour
{
private Vector3 mOffset;
private float mZCoord;
void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
//idk
mOffset = gameObject.transform.position - GetMouseWorldPos();
}
private Vector3 GetMouseWorldPos()
{
//Y X
Vector3 mousePoint = Input.mousePosition;
// Z COORD
mousePoint.z = mZCoord;
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void OnMouseDrag()
{
transform.position = GetMouseWorldPos() + mOffset;
}
}
thx
Nice
With that technique, you can drag the objects into the ground and into each other. Is there a way to avoid that?
seme hapens in hello neighbor never join 2 cans in that game
colliders or raycasts could help
@@createria2 imagine asking? lol
Thank you!