Click here to Skip to main content
15,070,974 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi there, This below code works absolutely fine : the DRAG AND DROP function works good. Here when correct image is droped on to its correct place it stays , but how to lock the image once its dropped in its correct position?


using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
    
 public class manager1 : MonoBehaviour 
    
 {
     
    public GameObject gorilla, dog, rabbit, lion, goat, gorillaBlack, dogBlack, rabbitBlack, lionBlack, goatBlack;
    
     Vector3 gorillaInitialPos, dogInitialPos, rabbitInitialPos, lionInitialPos, goatInitialPos;
    
     void Start()
    
    
    
     {
        gorillaInitialPos=gorilla.transform.position;
        dogInitialPos=dog.transform.position;
        rabbitInitialPos=rabbit.transform.position;
        lionInitialPos=lion.transform.position;
        goatInitialPos=goat.transform.position;
    
     }
    
    
    
 //1
     public void Draggorilla()
     {  
         gorilla.transform.position = Input.mousePosition;
             
     }
    
    
 //2
     public void Dragdog()
     {  
         dog.transform.position = Input.mousePosition;
     }
    
    
 //3
     public void Dragrabbit()
     {  
         rabbit.transform.position = Input.mousePosition;
     }
    
    
 //4
     public void Draglion()
     {  
         lion.transform.position = Input.mousePosition;
     }
    
    
 //5
     public void Draggoat()
     {  
         goat.transform.position = Input.mousePosition;
     }
    
    
    
    
    
 //1
    
 public void Dropgorilla()
     {
    
         float Distance = Vector3.Distance(gorilla.transform.position, gorillaBlack.transform.position);
         if (Distance < 50)
            
         {
             gorilla.transform.position = gorillaBlack.transform.position;
                 
         }
         else
         {
             gorilla.transform.position = gorillaInitialPos;
         
         }
    
     }
 //2
    
         public void Dropdog()
     {
    
         float Distance = Vector3.Distance(dog.transform.position, dogBlack.transform.position);
         if (Distance < 50)
         {
             dog.transform.position = dogBlack.transform.position;
                 
         }
         else
         {
             dog.transform.position = dogInitialPos;
         
         }
    
     }
 //3
    
 public void Droprabbit()
     {
    
         float Distance = Vector3.Distance(rabbit.transform.position, rabbitBlack.transform.position);
         if (Distance < 50)
         {
             rabbit.transform.position = rabbitBlack.transform.position;
                 
         }
         else
         {
             rabbit.transform.position = rabbitInitialPos;
         
         }
    
     }
 //4
    
 public void Droplion()
     {
    
         float Distance = Vector3.Distance(lion.transform.position, lionBlack.transform.position);
         if (Distance < 50)
         {
             lion.transform.position = lionBlack.transform.position;
                 
         }
         else
         {
             lion.transform.position = lionInitialPos;
         
         }
    
     }
 //5
    
 public void Dropgoat()
     {
    
         float Distance = Vector3.Distance(goat.transform.position, goatBlack.transform.position);
         if (Distance < 50)
         {
             goat.transform.position =goatBlack.transform.position;
                 
         }
         else
         {
             goat.transform.position = goatInitialPos;
         
         }
     }
 }


What I have tried:

hi there, This below code works absolutely fine : the DRAG AND DROP function works good. Here when correct image is droped on to its correct place it stays , but how to lock the image once its dropped in its correct position?
Posted
Updated 27-Sep-21 20:41pm
Comments
Pete O'Hanlon 27-Sep-21 11:15am
   
Are you trying to prevent it from being moved again? So, once it's dropped, that's it - it can't be moved ever again.
Ethir Pakalela 27-Sep-21 23:01pm
   
Hi! Thanks for the reply. YES its exactly what i need:

this is the updated code of the above one:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class manager1 : MonoBehaviour
{
     public GameObject[] animals;

    public GameObject[] blackAnimals;

    Vector3[] animalStartPos;

    Vector3[] animalBlackStartPos;

    void Start()

    {

        animalStartPos = new Vector3[animals.Length];

        animalBlackStartPos = new Vector3[blackAnimals.Length];

        for (int i = 0; i < animals.Length; i++)

        {

            animalStartPos[i] = animals[i].transform.position;

            animalBlackStartPos[i] = blackAnimals[i].transform.position;

        }            

    }

    public void dragAnimal(GameObject animal)

    {

        animal.transform.position = Input.mousePosition;

    }

    public void dropAnimal(GameObject animal)

    {

        int index = System.Array.IndexOf(animals, animal);

        float dist = Vector3.Distance(animal.transform.position, blackAnimals[index].transform.position);

        if (dist < 50)

            animal.transform.position = blackAnimals[index].transform.position;

        else animal.transform.position = animalStartPos[index];

    }
}


1 solution

There are many ways you could achieve this. You could, for instance, maintain a HashSet of animals that have been dragged like this:
C#
private HashSet<GameAnimal> _draggedAnimals = new HashSet<GameAnimal>
Having done this, you would need to do two things. First of all, in the if condition where you drop the animal, you would also add the animal to the HashSet.
C#
if (dist < 50)
{
  animal.transform.position = blackAnimals[index].transform.position;
  _draggedAnimals.Add(animal);
}
With this in place, you can prevent the animal movement by adding the following to the start of both the dragAnimal and dropAnimal methods.
C#
if (_draggedAnimals.Contains(animal))
{
  return;
}
In other words, if the hashset contains the animal, you have dragged and dropped it already so you don't want to do any more processing in your drag and drop methods.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900