Question by
wilhoran11 · Sep 29, 2017 at 09:02 AM ·
androidtouchdrag-and-drop
What to code if the laboratory equipments are correct and then proceeds to the next scene?
using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.SceneManagement; using UnityEngine.Events;
[System.Serializable] public struct Draggable { public string slotName; public Transform obj; }
[System.Serializable] public struct Slot { public Transform transform; public Transform correct;
private Transform clothObj;
public Transform cloth { get { return clothObj; } set { clothObj = value; } }
}
public class DressUp : MonoBehaviour { public float dropRange = 50.0f; public Slot[] slots; public Draggable[] draggables;
public UnityEvent OnComplete;
[HideInInspector]
public bool complete = false;
private Vector3[] initialPositions;
private int currentDrag;
void Start()
{
initialPositions = new Vector3[draggables.Length];
for (int i = 0; i < initialPositions.Length; i++)
{
initialPositions[i] = draggables[i].obj.position;
}
currentDrag = -1;
}
void Update()
{
if (currentDrag >= 0 && currentDrag < draggables.Length)
draggables[currentDrag].obj.position = Input.mousePosition;
complete = true;
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].cloth != slots[i].correct)
{
complete = false;
}
}
if (complete)
{
OnComplete.Invoke();
}
}
public void StartDrag(Transform cloth)
{
int dragIndex = -1;
for (int i = 0; i < draggables.Length; i++)
{
if (draggables[i].obj == cloth)
{
dragIndex = i;
break;
}
}
if (dragIndex == -1)
return;
Draggable drag = draggables[dragIndex];
Slot slot = new Slot();
int slotIndex = -1;
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].transform.name == drag.slotName)
{
slot = slots[i];
slotIndex = i;
break;
}
}
if (slotIndex != -1)
{
if (slot.cloth == cloth)
{
slots[slotIndex].cloth = null;
}
}
currentDrag = dragIndex;
}
public void Drop()
{
if (currentDrag < 0 || currentDrag >= draggables.Length)
return;
Draggable drag = draggables[currentDrag];
Slot slot = new Slot();
int slotIndex = -1;
for (int i = 0; i < slots.Length; i++)
{
if (slots[i].transform.name == drag.slotName)
{
slot = slots[i];
slotIndex = i;
break;
}
}
if (slotIndex != -1 && slot.cloth == null && Vector3.SqrMagnitude(slot.transform.position - drag.obj.position) < dropRange * dropRange)
{
drag.obj.transform.position = slot.transform.position;
slots[slotIndex].cloth = drag.obj;
}
else
{
drag.obj.transform.position = initialPositions[currentDrag];
}
currentDrag = -1;
}
}
[1]: /storage/temp/102812-screenshot-298.png
screenshot-298.png
(243.3 kB)
Comment
Your answer
Follow this Question
Related Questions
Drag and Drop Touch Collision 1 Answer
Dragged object not under finger on touch. 0 Answers
buttons vs touch. 1 Answer
Latency with android touch 1 Answer
Spawning enemies after death help 0 Answers