Hey all! I really hope some of you find this tutorial helpful in your own projects, as figuring it out for my own project was a great feeling. Thank you guys so much for watching!!
Пікірлер: 174
@marcelafregatto34564 жыл бұрын
Concise, simple and informative. Your explanation of the code was very useful. Thank you for the great tutorial!
@benmessenger96974 жыл бұрын
Thanks for ths. I'm brand new to Unity and this was very helpful. ALmost everything else I found had probably great scripts but none of them explained how to apply them. I had 2D rigid bosies, 3D rigid bodies, etc etc. Yours is the only one that explained it from the start, which made me realise I only needed a 2D box collider.
@steamingHOTsomething3 жыл бұрын
Thank you for describing what the code does in addition to showing us how to do this. It really helps with understanding the whole concept.
@davidgentry5971 Жыл бұрын
Thank you! I have been trying for days to get a click and drag method to work and yours is the first. Great tutorial!
@supgamen58324 жыл бұрын
Thank you so much it took me forever to find a video that worked I spent to hours just looking around on KZfaq thank you so much I really needed this your the best bro looking forward to more great videos
@OldJobobo2 жыл бұрын
Super awesome this is exactly what I needed and worked with very minor changes. Thank you so much.
@_.emberrr2 жыл бұрын
DUDE THANK YOU SO MUCH YOU DONT KNOW HOW MUCH I WANT TO THANK YOU IVE BEEN TRYING TO SEARCH FOR THIS FOR SO LONG THANK YOU THANK YOU
@cerielgerrits7812 жыл бұрын
Very clear and helpful Nade, thanks.
@austinthewotter4 жыл бұрын
Very nicely made and easy to follow. Thanks much.
@shahriarahmed7283 жыл бұрын
Fantastic tutorial. Very clear one & you made it easy to understand :)
@chadhurley20465 жыл бұрын
Great tutorial, man!!
@hairy_harrison5 жыл бұрын
Thanks for tutorial. It was great!
@amandaflood2 жыл бұрын
Thanks, very helpful and I like how you explain the code 😊
@Gernata_Dev3 жыл бұрын
thanks man !!! great tutorial
@cleotheocelot24134 жыл бұрын
Very good tutorial it really helped me keep up good content
@cormacconnell69614 жыл бұрын
Very helpful, thanks!
@arisaek7764 Жыл бұрын
A very helpful tutorial, this one is
@paolomontini9194 жыл бұрын
Best tutorial out there! :D
@projectoedit2 ай бұрын
Very good code! Very good explanation!
@gold3ngam3s844 жыл бұрын
great content, this saved me a lot of time. :D
@peanutman5221 Жыл бұрын
Thx soo much your a life saver my guy 🙏
@MDweller3 жыл бұрын
good job man. it help me a lot. thankyou
@rankoooo Жыл бұрын
Thx bro, it dose help a lot
@djpersonalspace24064 жыл бұрын
Best tutorial I've seen on this - how would i ensure that the objects are only moved within certain boundaries??
@ainurmaisarah83814 жыл бұрын
thank you for the tutorial.
@marcusfromoz70663 жыл бұрын
I've watched a lot of code tutorials in the last 12 months and your manner in this one is one of the best. Subscribed based on this video and will browse for any others I find useful. Thank you. By manner I mean you spoke clearly, you didn't stop/start to think things through, you clearly had it well planned out prior and you focused on making it as concise as possible.
@banane21863 жыл бұрын
.
@sipsopgamera76244 жыл бұрын
Thanks, you helped for a jam. My only feedback is to put the starpos varibles after we see the snaping issue so it is more clear. But still great video
@karacter33692 жыл бұрын
i like your tutorial a lot
@qwertykeyasmr64534 жыл бұрын
Thanks a lot. New subscriber gained
@scottlittle9345 жыл бұрын
THIS WAS A FREAKIN' LIFE SAVER!!! Thank you so much for making this tutorial and explaining everything so well. I'm a junior in college working toward my degree in game design and development and I'm in the middle of an experimental design challenge for class where I've got 2 weeks to make a game on my own, but I've got a week left since I was having a hard time coming up with an idea. Luckily, I thought of one a couple days ago and it hinged on drag and drop which I've never done before and everything I was finding either didn't explain it nearly as well as you and made it sound more complicated than this which left me more confused and until I found your video I was starting to think that it might be outta scope for the time I have left and that I'd be screwed. So, I just wanted to make sure I took the time to let you know how much I appreciate your help, and I'll try to remember to send you a message with with a link the finished game when it's done :D
@Faithexx2 жыл бұрын
2 years later.. how'd the project end up going? ;D
@user-eq6oy1um7e4 жыл бұрын
Great! I am a baby at coding but this is fairly simple.
@NotASpyReally4 жыл бұрын
Thank you SO MUCH for this !!! I liked and subscribed!
@Kutayeen4 жыл бұрын
A day just searched this, at least find it thanks a lot.
@residentsicko4 жыл бұрын
Great Info. I have a question/request for you if you'll indulge me. How would I implement a click and drag like this but also just a quick click with the same button triggers another event? Obviously so the event doesn't trigger when I click AND drag.
@azur73623 жыл бұрын
I am making a chain with multiple links, and I've watched turotials on it and gotten it together. Now i want to move the top part of the chain, but when I do, the whole thing disconnects from the part i grab. Any help?
@DonGT4 жыл бұрын
Question how can I make it so you can rotate the object by spinning it whilst holding it.. I have rigidbody2d on the object and stuff idk
@yced1013 жыл бұрын
excellent. THX
@DonGT4 жыл бұрын
Could I add a rigidbody 2d to make it flail around , also how would (okay so I have this minigame in my game that has you drag your character around and fall but how would i make it so that only his arms and legs flail but his head and body stay still)
@izzaacalley3 жыл бұрын
is there a way i could make it to where when i drag it over another object that can be dragged it only drags the one on top?
@Nathan-pe8kc4 ай бұрын
what if you wanted gravity to affect the sprite, so that once you release the mouse button it falls back to the ground?
@Toopa884 жыл бұрын
5:45 isn't OnMouseDown() only called on left mouse clicks to begin with?
@NadeOnRust5 жыл бұрын
Hey all! As of now, I cannot create community posts for all of you to see until I have 1,000+ subs, so to see a video schedule of each week, and to post suggestions, please click the "Discussion" tab of my channel to check that out. Thanks!!
@Empriction4 жыл бұрын
Just did everything, and when i try to click and drag, nothing happens? What did I do wrong? I included box colider and attached C# file to my sprite
@MrGidorMr4 жыл бұрын
Me too
@klaudijuspupsys34464 жыл бұрын
@@MrGidorMr @Empriction Input.GetMouseButtonDown(0) seem to be the problem for me. This explains why docs.unity3d.com/ScriptReference/Input.GetMouseButtonUp.html?_ga=2.96919455.1894432741.1592407429-119814026.1592407429
@ceeperderptoad27754 жыл бұрын
@@klaudijuspupsys3446 so remove it?
@klaudijuspupsys34464 жыл бұрын
@@ceeperderptoad2775 Yeah. Method OnMouseDown() is triggered by left mouse button. Use method OnMouseOver() if you want to choose which mouse button to click. private void OnMouseOver() { if (Input.GetMouseButtonDown(0)) Debug.Log("Pressed left click."); if (Input.GetMouseButtonDown(1)) Debug.Log("Pressed right click."); if (Input.GetMouseButtonDown(2)) Debug.Log("Pressed middle click."); }
@ceeperderptoad27754 жыл бұрын
@@klaudijuspupsys3446 thanks I also wrote some code somewhere in the comments and got an error I'm super new to unity as I started I think 4 days ago. If you can help with this too that would be amazing.
@mitar81064 жыл бұрын
Bravo!
@bottleflips36174 жыл бұрын
It’s working all except for when I release the mouse button the sprite doesn’t release, any idea why? I’m very new to this so it’s probably a stupid mistake
@letsplaynay99364 жыл бұрын
Thanks!
@hippolox54352 жыл бұрын
so one problem. When i give the item a rigidbody the velocity just keeps speeding up if held leading to some umm.... large events
@cerealboxgaming43492 жыл бұрын
How can i do this with rigidbody, and a collider? the rigidbody acts weird (spinning randomly) and the colliders don't work. (going through other objects with colliders) i would like this for my game death simulator.
@EdwardFominykh2 жыл бұрын
Any ideas on how to preserve velocity when you let go?
@ceeperderptoad27754 жыл бұрын
Edit: it was fixed but is there a way to add this to a ragdoll?
@vressentials3 жыл бұрын
Hi There, thanks for the video, unfortunately the Object I assigned the Script to moves after game start and am not able to move it whatsoever...no errors showing after tagging the Main Camera.
@vressentials3 жыл бұрын
Remember tag camera to MainCamera in the inspector or it'll say Null Object....
@zamanyolcusu15863 жыл бұрын
How can we use this kind of control on more objects using the tag function?
@Darkziss0 Жыл бұрын
Thank!
@sterling7092 жыл бұрын
the final part is good, where we have this startposx and y , to avoid sudden snap middle. thanks
@DSFtfghjfgf3 жыл бұрын
Everytime I type in the code it says somethings wrong I clicked it and It says theres an error on the 10th layer when nothing was there just the void Start thing. nevermind
@molecular_master3 жыл бұрын
Hey Nade! Can you tell me how to do this with rigidbody? I want to register collision so the dragged object won't go through another one
@ViralGymClips8743 жыл бұрын
Same
@MenaRamyEnaeet25123 жыл бұрын
Hey I sat Time.timeScale To zero and if I clicked the object and drag it to another position and tried to move it again it doesn't move solution please and Thank you for that awesome tutorial.
@deliriousmansano35564 жыл бұрын
Why theres is no monobehaviour. OnMouseDown and Up even I have box collider 2d??
@LEMNSQZY5 жыл бұрын
Hey! I got this working for a sprite, but nothing seems to happen if this same process is applied to a UI image on a canvas. The box collider and script remain unchanged, it's just the type of gameobject. Do you have any suggestions on how to fix this?
@grubinski134 жыл бұрын
I would like to know the same thing! Did you figure it out ?
@cricketfandom90893 жыл бұрын
kzfaq.info/get/bejne/eK2iYJp4v7CyiJs.html
@DavidAdochiti2 жыл бұрын
how can i maek it so that if i dont drag the object to the correct place, it goes back to its original position??
@user-iq4pc8ns9y4 жыл бұрын
tell me how to make it all work with the camera in perspective mode?
@youtuberyoutuber24954 жыл бұрын
I have a video request: It will build on this one. The request is, after taking your finger off the object, it is permanently placed, so you can't click on it to move it again. And in the same video, add an icon so we can delete any object on the scene after clicking on the object.
@younessaitouaddi22683 жыл бұрын
nice
@lex27634 жыл бұрын
Thanks for the great tutorial. I just had one question... When I am using this C# script the movement of the sprite lags behind the movement of the mouse (doesn't happen in your video)... I was wondering if you/anyone knows how to solve this?
@stuff38624 жыл бұрын
I think you have to set it so its not updating per frame or something and is just updating independent, I'll see if I can find it.
@yalld4 жыл бұрын
Any updates on how to fix this issue? I am having the same difficulties.
@windowsmoke4 жыл бұрын
@@yalld in the on mouse down void, change the line to "startPosX = mousePos.x * 130 - this.transform.loc...... Then do the same for y, mine is 70 (these are the numbers that work for me you might have a different number.) Next go to the update void and in the if function add the * 130 after mousePos.x and do the same for mousePos.y
@bikelovers50393 жыл бұрын
I also have this problem . Please tell me if you got solution ?
@bikelovers50393 жыл бұрын
@@yalld Sir i make a complete 2D game With this drag and drop method when our dragable objects quantity increase then it slow down drag movement under finger Very too much lag. Sir please Tell me i am fuck of on it for 10 days. But not find any solution ?
@vinodhbalakrishnan27263 жыл бұрын
hi Nade, Its very useful video, but i have a question.. why do you add box collider to that sprite, without adding the collider can we move or drag the object, if not could you explain why?
@Umblizm11 ай бұрын
This comment was 2 years ago but better late than never. Basically it needs. The collider because without it, it’s just a sprite. Without that component it’s just an image on the screen, so when you add the collider, it lets you click and drag it because your actually dragging the collider, the sprite is just attached to it
@gilgusinsky25134 жыл бұрын
If I wanted the rectangle to keep moving to the side a bit after I released, any tips on how that's done, I'm new to c#. What I want is to drag the square with mouse hold, then on release I want the square to keep moving to the side that you were dragging in, kinda like real life. I tried adding gravity to the square in unity but it only makes the square fall down
@gilgusinsky25134 жыл бұрын
i figured in out! its void moveCharacter(Vector2 direction){ rb.AddForce(direction * speed); }
@0x193 жыл бұрын
@@gilgusinsky2513 Make sure to do Rigidbody2D rb = getComponent(); and add the rb2d to the object, and disabling gravity!
@KoiMapache3 жыл бұрын
gracias, me a servido
Жыл бұрын
Is it possible to make this for mobile?
@peterschrimpel93753 жыл бұрын
thanks a lot
@harryfenteman62953 жыл бұрын
how would you snap them to a grid?
@topherblair50732 жыл бұрын
Does anyone here know how to get this to work with a perspective camera? Thanks
@lora69383 жыл бұрын
How to make a sprite come to the front on mouse click? Please 🙏
@WakeTheShark4463 жыл бұрын
I should point out that this runs into issues if you have other colliders on the parent or any children as those will set off the OnMouseDown event.
@tommytomato49433 жыл бұрын
i litteraly copied the code, but when i try it ingame it doenst work does any1 know why??????
@darredYouTube Жыл бұрын
Idk why but the script doesnt work. Without any errors. Just my gameObject doesnt want to work
@hansnystrompastor86904 жыл бұрын
Sorry, but when i drag the object it dosent seems to move under the cursor, instead, goes to the center of the screen, and kind of moves, but certainly not as it does on the video, any idea?
@hansnystrompastor86904 жыл бұрын
i was writing screentowievpoint by mistake loll D:
@letsplaynay99364 жыл бұрын
Is there a way when i drop the object, it moves with physics
@NadeOnRust4 жыл бұрын
Sure! Add a rigid body 2d, along with some sort of ground, and you can pick up and drop the sprite
@letsplaynay99364 жыл бұрын
Nade Oh thanks!
@TheLastApplePie4 жыл бұрын
Please help, It doesn't work. like i checked every line of code i have and him. i see no difference and it doesn't really work. the script is also attached to the sprite as well
@giji39393 жыл бұрын
Probably too late, but does your sprite have a collider?
@ayush213993 жыл бұрын
@@giji3939 in my, sprites jumps to center when clicked and don't move!!
@mymans11423 жыл бұрын
How come when using OnClick events the gameObjects dont collide?? Even with a boxcollider? I'm trying to do a puzzle thing where the pieces are blocked by one another, but they aren't colliding. Anyone know why?
@lukasziesemer82523 жыл бұрын
mabe because its always moving to the mouse and than it isnt working. Do you have a box collider, if yes is "is Trigger" active?
@elferchusmaymax81223 жыл бұрын
Did you solve it? I am trying something similar but I can't make it collide either
@tommytomato49433 жыл бұрын
i think you need a rigidbody to actually touch
@rik09044 жыл бұрын
finnaly some one who don't use canvas
@tetnyrotun39935 ай бұрын
i can still drag it even if i didnt put my mouse cursor on it, i just click on other spot and i can still drag it man
@mahirminhajk3 жыл бұрын
thazz love from India
@Chicano3000X4 жыл бұрын
When I click the object, it jumps to the corner of the canvas..
@spacevolvox40204 жыл бұрын
I had the same problem use this script by Artjom Schmitke private float startPosX; private float startPosY; private bool isBeingHeld = false; public bool isSnapping = false; private Vector3 mousePos; private void Update() { if(isBeingHeld == true) { GetMousePos(); if (isSnapping == false) gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0); else if(isSnapping == true) gameObject.transform.localPosition = new Vector3(mousePos.x, mousePos.y, 0); } } private void OnMouseDown() { if(Input.GetMouseButtonDown(0)) { GetMousePos(); if (isSnapping == false) { startPosX = mousePos.x - transform.localPosition.x; startPosY = mousePos.y - transform.localPosition.y; } isBeingHeld = true; } } void GetMousePos() { mousePos = Input.mousePosition; mousePos = Camera.main.ScreenToWorldPoint(mousePos); } private void OnMouseUp() { isBeingHeld = false; }
@platypus42 жыл бұрын
It doesnt work if i click it it only goes 1+ up
@theredlord61783 жыл бұрын
hey guys I have a little problem and was wondering if someone could help me, when I am starting to drag the object it becomes invisible in the game. However I can still see it in the scene and in the scene its not invisible... its invisible only in the game and only after I start dragging it... any help?
@JAMGGameryTutorialesramdom3 жыл бұрын
may be are you updating the Z value of the object so it moves the object out of the camera vision?? can be xD that's why the value for "Z = 0" no moves
@theredlord61783 жыл бұрын
@@JAMGGameryTutorialesramdom nope... Not at all... I was using transform.position = new Vector3(xPos, yPos); (if I am not wrong, if I was wrong I'll reply with another comment)
@theredlord61783 жыл бұрын
@@JAMGGameryTutorialesramdom hey, so I checked and thats what I was using: Vector3 location = Camera.main.ScreenToWorldPoint(Input.mousePosition); transform.position = location;
@JAMGGameryTutorialesramdom3 жыл бұрын
@@theredlord6178 exactly what i said, "Vector3 location = Camera.main.ScreenToWorldPoint(Input.mousePosition);"
@theredlord61783 жыл бұрын
@@JAMGGameryTutorialesramdom Thank you man!!! Hope in humanity still not lost! There are still good people who are willing to help!! Thank you !!
@yurchiie4 жыл бұрын
How does this work? like you're not even checking if the mouse is over the sprite? what am i missing
@dionesoloria37254 жыл бұрын
Check youre Camera projection to Orthographic
@OnyDeus4 жыл бұрын
I thought I was missing something at first. It must be a unity feature or something built in with sprites. A simple OnMouseDown(){ Debug.Log("Click"); } made me realize it just knows that it's being clicked.
@Atskas3 жыл бұрын
(41,11): error CS0106: The modifier 'private' is not valid for this item Any help? Here is the code. using System.Collections; using System.Collections.Generic; using UnityEngine; public class StickManDragger : MonoBehaviour { private float startPosX; private float startPosY; private bool isBeingHeld = false; // Update is called once per frame void Update() { if (isBeingHeld == true) { Vector3 mousePos; mousePos = Input.mousePosition; mousePos = Camera.main.ScreenToWorldPoint(mousePos); this.gameObject.transform.position = new Vector3(mousePos.x, mousePos.y, 0); } } private void OnMouseDown() { if (Input.GetMouseButtonDown(0)) { Vector3 mousePos; mousePos = Input.mousePosition; mousePos = Camera.main.ScreenToWorldPoint(mousePos); isBeingHeld = true; } private void OnMouseUp() { isBeingHeld = false; } } }
@NadeOnRust3 жыл бұрын
Your "private void OnMouseUp()" is inside the brackets for OnMouseDown(). Move it outside of the brackets and it will work
@Atskas3 жыл бұрын
@@NadeOnRust Is there a way to add this to my ragdoll?
@platypus42 жыл бұрын
Cna you also do it so you can rotate the game object please with a d you can rotate it a is right d is left
@sandeepanchoudhury-8j-1643 жыл бұрын
will it work for android
@zunuku41653 жыл бұрын
you could use OnMouseDrag()
@kenfontanilla2 жыл бұрын
what if I want to restrict it's movement to x axis only?
@Geshman_Sh54 Жыл бұрын
I also want tutorial on that
@ramongonzales55624 жыл бұрын
is this working in mobile phones. i mean the Mousedown will do the touch in the phone screen
@tinypizza42783 жыл бұрын
Is any one else having problems with not being able to move the platform
@N1ghtR1der6664 жыл бұрын
why do we need a vector3 in a 2d game? EDIT: I changed to vector 2d and its fine For anyone wanting to limit the axis of movement so for example you only want it to slide side to side then change the line un Update like below: this.gameObject.transform.localPosition = new Vector2(mousePos.x - startPosX, this.transform.localPosition.y);
@ramongonzales55624 жыл бұрын
i cant drag my object. its just change position when i clicked it .
@youtuberyoutuber24954 жыл бұрын
double check your code and make sure its exactly what he has.
@Toopa884 жыл бұрын
It didn't work for me either, maybe because I tried to move a GameObject in a canvas (the canvas is set to Screen Space - Camera with the main camera attached to it). The following worked for me if (isBeingHeld) { Vector2 mousePos = Input.mousePosition; mousePos = cameraMain.ScreenToWorldPoint(mousePos); mousePos -= new Vector2(startPos.x, startPos.y); parentObj.transform.position = mousePos; }
@anonymous140454 жыл бұрын
@ToFu, How about if i want to move the UI IMAGE (for inventory panel), i tried your code, but it still didn't work for UI ELEMENT...., do you have any solution for me ??
@samuv88642 жыл бұрын
Great tutorial and explaining! However, would you happen to know, how I could make this work the way, that if I have an object which is rectangle and it has gravity and I grab it from the bottom and drag it up somewhere where's no collider, it would flip it's self using the gravity. Sort of like "pinning" the object and rising/ moving it how the cursor moves. So if you were to spin the cursor the rectangle would spin due to G forces with rigidbody. Or if that's not explainable I'd like to know how I could make tossing possible. Like when I do a quick swipe to left and release mouse, the object would go flying that direction. If either of these are possible, it's fine and I'll see what I'll do. That would just make the feeling of the game I was working on much more panic punishing. Thanks in advance, if anyone was able to help! And thanks to you Nade for this great and easy tutorial!
@the_terraria_guy9010Ай бұрын
the if(isBeingHeld == true) kinda killed me inside, but otherwise great tutorial!
@4orniy4683 жыл бұрын
Give the script pls unity says error
@windowsmoke4 жыл бұрын
One question I have is: how do I make it so it can't go through walls
@NadeOnRust4 жыл бұрын
Add a rigidbody to the sprite
@windowsmoke4 жыл бұрын
@@NadeOnRust I have one but since transform is pretty much teleporting it to the mouse it doesn't care about walls. Basically in the same way as in the scene editor
@windowsmoke4 жыл бұрын
@Jay Dev huh? But it is teleporting. That doesn't care if there's a wall or not what I need is some sort of way to make the object gravitate towards the mouse
@elferchusmaymax81223 жыл бұрын
@@windowsmoke Did you solve this problem? I am trying to make the same thing but I can't make it either