Unity2D: Placing objects down in a controlled area
I've created a 2d box collider within my game in order to contain any objects (with a certain tag) dragged within the boxed area, if the object is dragged outside of the box collider then it is automatically sent back to it's original position. My problem: I have two different objects, one - the original object & two - the clone object (originally both object starts off at the same position out side of the 2d box collider area). So if the original object is placed down in the box collider area, when clicking and dragging the clone object into the box collider area it is sent back to it's start position before it even reaches the box collider area! I know it has something to do with the object being a clone, but that's the way my game functions so I can't get rid of my clone objects and make separate objects. So does anyone know how I can click and drag my object (original and instantiated) into the box collider area without it being sent back unless if I actually drag the object outside of the box collider!
My Script:
public bool isPlaced = false;
public Transform Cloneitem;
public GameObject item, slotHolder;
void Start () {
startPos = transform.position;
}
// Update is called once per frame
void Update () {
if (isPlaced == true) {
Cloneitem.transform.parent = null;
item.transform.localScale = new Vector3 (0.3f, 0.3f, 0);
rend1.sortingOrder = -1;
}
Vector3 pos = transform.position;
pos.z = 1;
transform.position = pos;
Debug.Log (" " + pos.z);
}
void OnTriggerEnter2D (Collider2D col) {
if (col.tag == "Area") {
isPlaced = true;
}
}
void OnTriggerExit2D (Collider2D col) {
if (col.tag == "Area") {
isPlaced = false;
Debug.Log ("Sent Back");
isOut = true;
checkPos = true;
Cloneitem.transform.parent = GameObject.FindGameObjectWithTag ("CloneSlot").transform;
item.transform.position = startPos;
if(playingGame == true){
item.transform.localScale = new Vector3 (23f, 24f, 0);
}
rend.sortingOrder = 1;
}
}
Your answer
Follow this Question
Related Questions
Stuck With this codes. 1 Answer
Spawning a prefab at a specific point in a procedurally generated map 1 Answer
How To Generate Platforms Down Each other in a pattern with some different positions Unity 1 Answer
Instantiate GameOBject with changing size in relation to another GameObject in the scene C# 0 Answers
Instantiated Prefab not Moving 1 Answer