Good morning,
Recently I’ve upgraded my project to unity 5 and encountered some problems. One of them I am not able to fix myself.
In the project, a gameobject is able to be dragged around by the user and onto an other static gameobject. When the user releases the object, a script checks for collisions. This used to work before, but not anymore in unity 5.
Below part of the script used by the dragged object to follow the mouse/input of the user.
IEnumerator folowMouse(){
GetComponent<Image> ().color = Color.gray;
GetComponent<BoxCollider2D> ().enabled = false;
GetComponent<BoxCollider2D> ().size = new Vector2 (2, 2);
while (Input.GetMouseButton(0)) {
clicked = true;
Vector3 pos = Input.mousePosition;
pos.z = 10;
pos = Camera.main.ScreenToWorldPoint(pos);
transform.position = pos;
yield return 0;
}
Camera.main.transform.FindChild ("Audio").transform.GetChild (1).GetComponent<AudioSource> ().Play ();
GetComponent<BoxCollider2D> ().enabled = true;
for (int i=0; i<10; i++) yield return 0;
transform.position = basePosition;
GetComponent<BoxCollider2D> ().size = new Vector2 (30, 30);
GetComponent<Image> ().color = Color.white;
clicked = false;
}
The Boxcollider is disabled and enabled to only trigger when the mouse is released. After enabling (line 18), there is a small delay (line 19), than the object is returned to its original position. The dragged object has a rigidbody as well.
On the static gameobject, there is code to handle collisions.
void OnTriggerEnter2D(Collider2D coll){
if (coll.gameObject.tag == "Letters") {
if(coll.GetComponent<LetterButtons>().clicked){
text.text = coll.GetComponent<LetterButtons>().letter.ToUpper();
hasInPut = true;
if(juisteText.Equals(text.text)){
isJuist = true;
}else{
isJuist = false;
}
StartCoroutine(game.GetComponent<MainGameScript>().letterInput());
}
}
}
The strange thing is, when the disabling the BoxCollider on the dragged object (line 3) is commented out (so the boxcollider is always enabled), a collision is detected and works as it used to.
I’ve tried to increase the delay before the dragged object is moved back, and there are clearly some moments when the collider is enabled and on the static object.
I hope I’ve explained the situation enough, otherwise feel free to ask.