- Home /
Simple Looping drag and drop game
hi, I'm trying to do a simple drag and drop game for my nephew (maybe can publish it in playstore). Because of that I will work with so many items I don't want to write code for each of them. I want to make a loop to find them by their index number and move to it's place with the same index number. this is the code I came up with. but it's not working and giving me theese errors; IndexOutOfRangeException: Index was outside the bounds of the array. MouseDrag.Start () (at Assets/MouseDrag.cs:22) Assets\DragAndDrop.cs(10,25): warning CS0649: Field 'DragAndDrop.initialPosition' is never assigned to, and will always have its default value null
I'm sure there are many other errors that I don't know. is there any body here have an idea or could show me how to do it. I'm desperately need any kind of help now.
Here is the code I tried to write:
`public class MouseDrag : MonoBehaviour { public GameObject[] objects;
public Transform[] objectPlace;
public List <Vector2> initialPosition = new List<Vector2>();
private Vector2 mousePosition;
private float deltaX, deltaY;
int i = 0;
public static bool locked;
private void Start()
{
for(int i=0; i< objects.Length; i++)
initialPosition.Add(new Vector2());
initialPosition[i] = objects[i].transform.position;
}
void OnMouseDown()
{
metod(i);
deltaX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
deltaY = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
}
void OnMouseDrag() {
if(!locked){
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector2(mousePosition.x - deltaX, mousePosition.y - deltaY);
}
}
void OnMouseUp()
{
if(Mathf.Abs(transform.position.x - objectPlace[i].position.x) <= 0.5f &&
Mathf.Abs(transform.position.y - objectPlace[i].position.y)<= 0.5f)
{
transform.position = new Vector2(objectPlace[i].position.x, objectPlace[i].position.y);
locked = true;
}
}
public int metod(int i)
{
for(int p = 0; p < objects.Length; p++)
if ( !locked)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider.gameObject == objects[p].gameObject){Debug.Log("index number is: " + objects[p].transform.position.ToString());}
p = i;}
return i;
}
}
Answer by dan_wipf · Feb 24, 2019 at 07:27 PM
2 things wrong here on a wuick overview:
delete the line initialposition.add and ad directly the position =
initialPosition.Add(objects[i].transform.position);
metod i is wrong, you call it in the update section with i. is never asigned or a valid int. delet int i in metod because its unecessary, instead return p directly
Edit, Final working Script:
using UnityEngine;
using System.Collections.Generic;
public class MouseDrag : MonoBehaviour{
public float distanceToEndPos = 1;
public List<GameObject> objects = new List<GameObject>();
public List<Vector2> initialPosition = new List<Vector2>();
public List<Vector2> desiredPosition = new List<Vector2>();
Vector3 mousePos;
Transform curObj;
int curIndex;
bool isDraging = false;
void Start(){
objects.AddRange(GameObject.FindGameObjectsWithTag("DragAndDropObjects"));
foreach(GameObject g in objects){
initialPosition.Add(g.transform.position);
}
GameObject[] desObj = GameObject.FindGameObjectsWithTag("DragAndDropEndPos");
foreach(GameObject g in desObj){
desiredPosition.Add(g.transform.position);
}
}
void Update(){
mousePos = GetMousePos();
if(!isDraging){
GetRayCast();
}
if(isDraging){
DragAndDrop();
}
}
public void GetRayCast(){
RaycastHit2D hit = Physics2D.Raycast(new Vector2(mousePos.x,mousePos.y),Vector2.one);
if(hit.collider != null && Input.GetMouseButtonDown(0) && hit.transform.tag == "DragAndDropObjects"){
for(int i = 0; i<objects.Count;i++){
if(hit.collider != null && Input.GetMouseButtonDown(0) && hit.transform.gameObject == objects[i]){
curIndex = i;
curObj = objects[i].transform;
isDraging = true;
}
}
}
}
public Vector3 GetMousePos(){
Vector3 mp = new Vector3();
mp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
return new Vector3(mp.x,mp.y,0);
}
public void DragAndDrop(){
if(curObj != null){
if(Input.GetMouseButton(0)){
curObj.position = mousePos;
}
if(Input.GetMouseButtonUp(0)){
if(Vector2.Distance(mousePos,desiredPosition[curIndex]) <= distanceToEndPos){
curObj.position = desiredPosition[curIndex];
curObj = null;
isDraging = false;
}else{
curObj.position = initialPosition[curIndex];
curObj = null;
isDraging = false;
}
}
}
}
}
edit: it's working know. Thanks man I was working on this script about two weeks. you did it in a couple hours @dan_wipf
@pascal07nouma I did as quick edit of your Code. => untested, but should work. But I think there are more Problems in your code (logical ones, than you think of). for example you Debug in the int method() index number is: + objects position as a Vector actual the index number is simple i.
as well in your Titel it says a Drag and Drop game. but I can't see when you actually Drag any Thing except the Transform this Script is attached to. So right now the RayCast and the List of Objects is kind of unused just for the Position where this Transform should be Placed next on $$anonymous$$ouseUp..
I recommend you to re think what you actually want to achieve. but you can have a chat over here https://discord.gg/kdEbm3V
ah and I believe you got as well an error through the RayCast. if the Ray's detecting something else than in you array of objects it causes troubles so as I mentioned re think what you want to do!