Unity Tutorial : Drag Gameobject with Mouse

preview_player
Показать описание
In this Unity Gamedev tip and tutorial I show how to drag a gameobject around in your scene while the game is running.

I write a C# script that you can attach to you game object to have the drag feature.

The script can be found here, you can use it in your won Unity projects:

See my social profiles here

Рекомендации по теме
Комментарии
Автор

This is exceptional. Clear, fast, clean and massively useful. I'm now prompted to look at a lot more by the author.

garethdenyer
Автор

how to fix the objects clipping through the ground? while you are dragging it

AidanDemps
Автор

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

AstonArchitecture
Автор

Just a little adaptation to fulfill my implementation and its working like a charm. Thanks a lot!!

VictorAlmeidaTroo
Автор

OMG I finally found something that works! Thank you!

yechan_naesarang
Автор

Simple and working amazing still in 2022 :)

joshuadrewlow
Автор

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

jakubmajewski
Автор

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?

slmjkdbtl
Автор

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.

unityfuchs
Автор

Very nice and clear tutorial ! Thanks !

Edrenn-live
Автор

Do you happen to have a similar tutorial for touch input?

CMichaelNeely
Автор

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?

daryaf
Автор

PERFECT NOW I CAN MAKE AN amnesia like door!

AidenWolf
Автор

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

Waarriorgaming
Автор

Will this be compatible with mobile devices or touchscreens?

PowBro
Автор

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!

cydoyk
Автор

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?

SharkfightersSH
Автор

hey uhh... i want it to have collision with other objects while i hold it.. how do i?

byter-exe
Автор

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 :/

artjom
Автор

Do you know how to lock it so it doesn't move the object in the y axis?

crab
visit shbcf.ru