• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dumbkingdoughnut7 · Oct 10, 2022 at 12:07 AM · unity 2ddrag-and-dropundodrag and dropdrag objects

Undo drag and drop

Hello I want to implement an undo button where if the player drags an object and drops it anywhere and wants to bring it back to its original position, they can use the undo button. I have already made a script for dragging and dropping game objects but I don't know how to undo them. Any help is appreciated.

Drag and drop code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DragnDrop : MonoBehaviour
 {
     private float startPosX;
     private float startPosY;
     private bool isBeingHeld = false;
 
      void Update()
     {
         if(isBeingHeld)
         {
             Vector3 mousePos;
             mousePos = Input.mousePosition;
             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
 
             t$$anonymous$$s.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
         }
     }
 
     private void OnMouseDown()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Vector3 mousePos;
             mousePos = Input.mousePosition;
             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
 
             startPosX = mousePos.x - t$$anonymous$$s.transform.localPosition.x;
             startPosY = mousePos.y - t$$anonymous$$s.transform.localPosition.y;
 
             isBeingHeld = true;
         }
         
     }
 
     private void OnMouseUp()
     {
         isBeingHeld = false;
     }
     
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image logicandchaos · Oct 12, 2022 at 10:34 PM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SurreyMuso · Oct 10, 2022 at 08:35 AM

Unless there are other variables that are changed as a result of the drag, all you need to do is store the starting position in the OnMouseDown method. When you undo, move the game object back to the original position. If you have events triggered or other variables changed or other callbacks (OnCollision or OnTriggerEnter, for example), it gets more complex and you potentially have to undo rather a lot of changes.


You might wonder how t$$anonymous$$s is done in games development or how an undo $$anonymous$$story works in productivity tools or, for example, how does a race replay work at the end of a racing game. They use what is called the Command Pattern, where every input is stored in a list. The main part of the Update routine is simply collecting all these inputs and adding them to the list. Then there are matc$$anonymous$$ng forward and backward functions to process each of these inputs. Forward for normal processing and the replay function; backward for single or multiple undos. There is always a list of every input that the user made - each press of a button on the keyboard, every wiggle of the joystick etc. The entire game can be replayed or wound back.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dumbkingdoughnut7 · Oct 11, 2022 at 08:05 PM 0
Share
avatar image
0

Answer by Triqy · Oct 11, 2022 at 08:14 PM

 Transform _oldPosition;

 void OnMouseDown()
 {

     // store your old position in a vector 3
     _oldPosition = yourObjectsPosition;

 }


 // Set up your undo button OnClick to move your object back to old position
 public void OnButtonPress()
 {

     yourObjectsPosition = _oldPosition;

 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dumbkingdoughnut7 · Oct 17, 2022 at 10:22 PM 0
Share
avatar image
0

Answer by SurreyMuso · Oct 12, 2022 at 01:48 PM

Do you just have a single object that is dragged? If so, you should use a Stack to store successive positions. Stacks can store data of a fixed type in a way that if you "push" onto the stack, the value goes on the top. Then if you 'pop', you get the last item that was put onto the stack.

So, when you start a drag (possibly when you get MouseDown), push the transform.position onto the stack. When you undo, you pop from the stack and set the transform position.

Just one more t$$anonymous$$ng, if you try and pop from an empty stack, you'll get an error so there's a little trick that checks first.

     Stack<Vector3> undoData = new Stack<Vector3>();        
 
     void OnMouseDown()
     {
         undoData.Push(transform.position);
     }
 
     void Undo()
     {
         Vector3 undoPos;
         if (undoData.TryPop(out undoPos))
         {
             transform.position = undoPos;
         }
     }


All t$$anonymous$$s code should sit on your draggable object. The Undo button should point at the Undo method. Do you fancy getting cute? If so, disable the Undo button when the stack is empty. You can use undoData.count to tell you how many items there are in the stack.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dumbkingdoughnut7 · Oct 12, 2022 at 07:36 PM 0
Share
avatar image SurreyMuso dumbkingdoughnut7 · Oct 12, 2022 at 10:15 PM 0
Share
avatar image dumbkingdoughnut7 SurreyMuso · Oct 12, 2022 at 10:24 PM 0
Share
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

164 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drag and drop picks an object too far away from mouse position 2 Answers

Following instruction for drag and drop 3d object unity but not working 1 Answer

spawn and drag game object without leaving mouse button 1 Answer

Destroy gameobject after drag into the correct slot 0 Answers

How can i destroy an object in OnPointerEnter ( IPointerEnterHandler ) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges