- Home /
Ball getting stuck between two box colliders
I am making a puzzle game where the player has the objective of taking the ball into lower pipe. He can do this by moving the whole row or whole column alternatively every turn respectively.
But i am facing this weird issue where the ball gets stuck between two Box colliders while making a move (As you can see in the GIF Image)
This blue ball is using circle collider 2D and a rigidbody2D The boxes consist of boxcollider2D and surface Effector 2D(Since i want ball to move automatically when there is space in front)
I seriously don't know how to solve this issue, i have searched forum for solution but i got no luck with those solutions!!
I will be really glad if someone can help me with this issue :) !!
Edit 2: Here is the small piece of code that handles the boxes as requested by @tormentoarmagedoom , it is kind of hard to explain code since it is very large algorithm consisting of many scripts, but i will try my best to explain!!
I am using MVC structure(Model view controller). The boxes in the gif image are just for purposes of display and the movement you see is controlled by a script i.e, the model. The boxes are represented as a 2D array form and all the controlling of 2D array which is not seen by user is done by other script in background i.e, the controller. the controller is managing the 2D Array.
public void boxRCSwap(GameObject selectedObj, bool X,bool right,bool up)
{
GameObject target = null;
int[] p = findElementPositionInArray(selectedObj); //This function is finding the array position of box the player touched
if (selectedObj != null)
{
if (X)
{
for (int i = 0; i < columns; i++)
{
target = boxes[p[0], i];
if (target != null)
{
waitTime = 0;
if (right)
StartCoroutine(Transition(target, (target.transform.position.x + constNumberX), p, true));
else if (!right)
StartCoroutine(Transition(target, (target.transform.position.x - constNumberX), p, true));
}
}
}
else
{
for (int i = 0; i < rows; i++)
{
target = boxes[i, p[1]];
if (target != null)
{
waitTime = 0;
PlayerMechanics.movesCounter();
if (up)
StartCoroutine(Transition(target, (target.transform.position.y + constNumberY), p, false));
else if (!up)
StartCoroutine(Transition(target, (target.transform.position.y - constNumberY), p, false));
}
}
}
reArrangeArray(X, p, right, up); //This function is rearranging the 2D Array according to changes player made
}
}
Basically i am lerping the boxes in order to move them from one position to other for example box[0,0] moving to box[0,1]
private IEnumerator Transition(GameObject target, float X, int[] p,bool xAxis)
{
while (waitTime < waitForSeconds)
{
if (xAxis)
{
target.transform.position = Vector2.Lerp(target.transform.position, new Vector2(X, target.transform.position.y), (waitTime / waitForSeconds));
}
else
{
target.transform.position = Vector2.Lerp(target.transform.position, new Vector2(target.transform.position.x, X), (waitTime / waitForSeconds));
}
waitTime += Time.deltaTime;
// yield return new WaitForSeconds(0.1f);
yield return null;
}
}
This is inspector of ball and the crates as requested by @TanselAltinel
inspector of crate Click here
Inspector of ball Click here
Note :- Ballbehaviour script is doing nothing but checking axis of y if it's dropped in that acid or not!!
Can you please show editor inspector for ball and crates?
Yes, and can you post how the boxes are moved in the code? They are $$anonymous$$inematic? $$anonymous$$oved by forces..?
Thanks.
@TanselAltinel @tormentoarmagedoom i updated the post, please check it :) !!
You're moving objects with updating transform values, which confuses physics engine. I think that's the problem.
So what should be possible solution for that??