Hi,
i’m trying to create a drop zone script which i’m gonna use for my drag and drop script. At the moment i can’t really detect the dropzone when my mouse goes over it. My main problem is that i’d like to add a collider to the gameObject BUT how to actually resize the collider so it has the right size considering how many children it has (You’ll understand the thing with the children if you read my code) ? All my script does for the moment is putting it’s children in order. So what do you think ? Should i use another method ? It’s for a 2d card game by the way (classic poker-like) and i would use it for holding the player’s cards in a neat order ^^. I know i could use a panel but i’d like to avoid that.
Here is my script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DropZone : MonoBehaviour {
[SerializeField]
float radius = 10;
[SerializeField]
float offset;
[SerializeField]
int _nbElements;
[SerializeField]
int _maxNbElements;
// [SerializeField]
// List<Transform> childs = new List<Transform> ();
public bool canAdd {
get{ return _nbElements > _maxNbElements; }
}
void arrangeChilds(){
updateNbElement ();
if (_nbElements == 0)
return;
else if (_nbElements == 1) {
for (int i = 0; i < _nbElements; ++i) {
Transform child = transform.GetChild(i);
if (child != transform)
child.position = transform.position;
}
} else {
float distance = ((radius + offset) / _nbElements);
float startX = (0 - (radius / 2)) + (distance / 2);
for (int i = 0; i < _nbElements; ++i) {
Transform child = transform.GetChild(i);
if (child != transform) {
child.localPosition = new Vector3 (startX, 0f, 0f);
startX += distance;
}
}
}
}
void updateNbElement (){
_nbElements = transform.childCount;
}
void Start(){
arrangeChilds ();
}
}