- Home /
Question by
AyanRoy · Jun 23, 2020 at 08:41 PM ·
gameobjectarraylistdrag-and-drop
How to drag and drop any object to any empty space?
I am trying to make a drag and drop game. For example there is a car with 3 empty spaces- BodySpace, Wheel_Space1 and Wheel_Space2. I have 3 objects which will fit in these empty spaces- Car body, Wheel 1 and wheel 2.
Right now the code works perfectly but Wheel 1 can only be placed at wheel_Space1 and wheel 2 can only be placed at wheel_Space2.
How can I make it work so that any wheel can be placed on any empty wheel space?
Hope I made my question easy to understand. I am still a noob in coding and need help on this please. please check the image here- https://pasteboard.co/JesHJzxF.jpg
This is the script I have on the wheels- where pictureOnePos is the wheel itself and picture Two is the empty space where it can be placed.
using UnityEngine;
using UnityEngine.EventSystems;
public class DragManager : MonoBehaviour, IDragHandler, IEndDragHandler
{
Vector2 pictureOnePos;
public GameObject pictureTwo;
//Audio
public AudioSource source;
public AudioClip[] correct;
public AudioClip incorrect;
public bool locked;
// Start is called before the first frame update
void Awake()
{
locked = false;
}
void Start()
{
pictureOnePos = transform.position;
}
// Update is called once per frame
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventDate)
{
float Distance = Vector3.Distance(transform.position, pictureTwo.transform.position);
if(Distance < 300 && !locked)
{
transform.localScale = pictureTwo.transform.localScale;
transform.position = pictureTwo.transform.position;
source.clip = correct[Random.Range(0, correct.Length)];
source.Play();
GameController.instance.picInPlace++;
locked = true;
transform.GetComponent<DragManager>().enabled = false;
}
else
{
source.clip = incorrect;
source.Play();
transform.position = pictureOnePos;
}
}
}
Comment