Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Drag and replace

Discussion in 'Scripting' started by ganesh_be, Mar 21, 2020.

  1. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    Hi,
    I am working on a 3d game where I am adding objects to my inventory. I am also able to drag objects out of inventory and place it at any location where the mouse points at. I am just wondering how do I replace an object with the objects present in my inventory.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    What you're asking for is extremely specific to however you're doing your inventory and equipped item code. In general, I'd expect that when you "drop" the item, you'd detect whether the mouse is over another item, and then do whatever logic is required to swap them. Did you write the drag/drop code yourself? Or are you working on some code that someone else wrote?
     
  3. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    Thanks for your reply. I was working on code written by someone else as I am working on the code for the first time. But somehow I managed to replace a different object placed in the plane with the object present in my inventory. The only problem I am facing now is that whenever I am dragging the object from inventory and not hitting the new object with my mouse cursor the object present in the inventory moves to the original location from where I have picked up my object but I want that the object to stay in the inventory rather than moving to its original position.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Photo : InventoryItemBase
    6. {
    7.     public GameObject replace;
    8.  
    9.     public Inventory inventory;
    10.  
    11.     public HUD Hud;
    12.     // Start is called before the first frame update
    13.     public override string Name
    14.     {
    15.         get
    16.         {
    17.             return "Photo";
    18.         }
    19.     }
    20.  
    21.     public override void OnDrop()
    22.     {
    23.         RaycastHit hit = new RaycastHit();
    24.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.         replace = GameObject.FindGameObjectWithTag("replace");
    26.         if (Physics.Raycast(ray, out hit, 1000))
    27.         {
    28.             //TODO item drop functionality on a fixed wall
    29.          
    30.             if (hit.collider.gameObject.CompareTag("replace"))
    31.             {
    32.                 gameObject.SetActive(true);
    33.                 gameObject.transform.position = replace.gameObject.transform.position;
    34.                 gameObject.transform.rotation = replace.gameObject.transform.rotation;
    35.                 Destroy(replace.gameObject);
    36.              
    37.             }
    38.             else
    39.             {
    40.                // Note sure
    41.             }
    42.        
    43.         }
    44.     }
    45. }
     
    Last edited: Mar 24, 2020
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Well, I'm not sure if this is the optimal way to do drag/drop. But to fit into your current approach, you could make all the droppable slots contain a game object, and you could test whether the raycast hit a game object of that type. For example:
    Code (CSharp):
    1. else if (hit.collider.gameObject.CompareTag("EmptySlot")) {
    2.  
    3. }
    Another approach would be to have the entire inventory area be a single game object, and than you could convert the position of the hit to coordinates, and decide which inventory slot the item belongs in.
     
  5. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16
    1) Initial Scene
    View attachment 583558

    2) When I press a key and the object is added to inventory
    upload_2020-3-24_21-50-3.png

    3) When I drag and drop the object from inventory on second object

    View attachment 583561

    4) When I drag the object from inventory and place it at some other position the object moves to original position from where I picked but I want to keep in the inventory only
    upload_2020-3-24_21-46-46.png
     
    Last edited: Mar 24, 2020
  6. ganesh_be

    ganesh_be

    Joined:
    Mar 10, 2020
    Posts:
    16

    But I think that is not my requirement or maybe I didn't get you. I have added a few more details as a part of an explanation.
     
    Last edited: Mar 26, 2020