- Home /
Unity 4.5.2p2 SliderJoint2D broken or different?
We have a quite big 2D game at around 80% of its development completed (now it has 81 scenes). It was working fine on Unity 4.3, but we needed to upgrade to Unity 4.5 due to a pair of its new features. The problem is that all the prefabs that were sliding platforms where a ball could bounce over them (using physics2D) now are broken. They use a Slider Joint 2D with motor and limits to make them move horizontally or vertically over the scene (a custom script makes them switch direction when a limit is reached). They are connected to a point in the scene ("Connected Anchor") to make a line of movement. Now they stop well before a limit is reached and if we look at their gizmo, the limits are still well calculated but now they seem to move with the object! We even tried to update the anchor in FixedUpdate to keep the limits on their place, but it's a no-no.
So, might it be that SliderJoint2D behaves different than on 4.3? Or could it be broken in this build? Is some other developer using it in a similar way and noticed the problem?
Here is the code that calcs the limits (please notice that it worked flawlessly under 4.3):
void CalcSliderData()
{
JointTranslationLimits2D limits;
RaycastHit2D hit;
Vector2 startPos;
// Calc/load positions and limits
mSliderJoint.connectedAnchor = this.transform.position;
limits = mSliderJoint.limits;
if (Direction == MoveDirection.Horizontal)
{
// (cast ray rightmost and calc max limit)
startPos = this.transform.position;
startPos.x += this.renderer.bounds.size.x / 2 + 0.01f;
hit = Physics2D.Raycast(startPos, Vector2.right,
float.PositiveInfinity, 1 << 10);
if (hit.collider != null)
{
limits.max = (hit.point - startPos).x - BorderCollisionSensitivity;
}
else
{
mSliderDataIsOK = false;
print("ERROR: MovingWallHoriz " + this.name +
" can't find its rightmost limit!");
return;
}
// (cast ray leftmost and calc min limit)
startPos = this.transform.position;
startPos.x -= this.renderer.bounds.size.x / 2 + 0.01f;
hit = Physics2D.Raycast(startPos, -Vector2.right,
float.PositiveInfinity, 1 << 10);
if (hit.collider != null)
{
limits.min = (hit.point - startPos).x + BorderCollisionSensitivity;
}
else
{
mSliderDataIsOK = false;
print("ERROR: MovingWallHoriz " + this.name +
" can't find its leftmost limit!");
return;
}
mSlideUpperPos = mSliderJoint.connectedAnchor;
mSlideUpperPos.x += limits.max;
mSlideBottomPos = mSliderJoint.connectedAnchor;
mSlideBottomPos.x += limits.min;
}
else
{
// (cast ray upwards and calc max limit)
startPos = this.transform.position;
startPos.y += this.renderer.bounds.size.y / 2 + 0.01f;
hit = Physics2D.Raycast(startPos, Vector2.up,
float.PositiveInfinity, 1 << 10);
if (hit.collider != null)
{
limits.max = (hit.point - startPos).y - BorderCollisionSensitivity;
}
else
{
mSliderDataIsOK = false;
print("ERROR: MovingWallVert " + this.name +
" can't find its upper limit!");
return;
}
// (cast ray downwards and calc min limit)
startPos = this.transform.position;
startPos.y -= this.renderer.bounds.size.y / 2 + 0.01f;
hit = Physics2D.Raycast(startPos, -Vector2.up,
float.PositiveInfinity, 1 << 10);
if (hit.collider != null)
{
limits.min = (hit.point - startPos).y + BorderCollisionSensitivity;
}
else
{
mSliderDataIsOK = false;
print("ERROR: MovingWallVert " + this.name +
" can't find its lower limit!");
return;
}
mSlideUpperPos = mSliderJoint.connectedAnchor;
mSlideUpperPos.y += limits.max;
mSlideBottomPos = mSliderJoint.connectedAnchor;
mSlideBottomPos.y += limits.min;
}
mSliderJoint.limits = limits;
mSliderDataIsOK = true;
}
Can you show the part of script which deter$$anonymous$$es limits?
Of course. You can find it in the question (I edited it to add the code). Thank you!
Answer by Hazlo · Jul 28, 2014 at 07:09 AM
Ok ok, I'll answer to my own question: from Unity 4.5, motors are REVERSED. So the speed needs to change the sign to be the same as before, AND the limits need to switch. So, to put it easy (of course that won't be final code!), if you add these lines:
float a = limits.min;
limits.min = -limits.max;
limits.max = -a;
before "mSliderJoint.limits = limits", everything is back to what it was in 4.3. Woah, what a change! It's just like if I take my car to the mechanic to change oil and when I get it back, from now on the Reverse is forwards and Drive is Reverse!