Search code examples
c#unity-game-engine3ddrag-and-dropinventory

Drag and drop just like Gamefox game


I want to make a game where player can customize car by dragging parts over the car from inventory just like in this video(My game is 3d).I have done all other part of programming but stuck at drag and drop function.

I apologize if it was asked before and also for my English if I made any mistake, because it is not my native language.

thank you in advance for your time and consideration.

https://www.youtube.com/watch?v=EdgvVZ7FlOg


Solution

  • Okay, then here is the following example for you, for moving a GameObject around. It could be possible that Input.mousePosition is in Screen coordinates and not in world coordinates. If this is the case you would need to translate them first (you will find how to achieve this using google - I already read threads about this):

        private bool dragged = false;
    
    // Always called when mouse is ober the object
    void OnMouseOver () {
        //Check if left mouse button is pressed
        if(Input.GetMouseButtonDown(0)) {
            //mark object as dragged
            dragged = true;
        }
    }
    
    void Update() {
        if(dragged) {
            transform.position = Input.mousePosition;
    
            if(Input.GetMouseButtonUp(0)) {
                dragged = false;
            }
        }
    }