Hi all,
I’m trying to translate a GameObject only on its self y axis but it get translated slightly on x and z axes too.
Here is my code :
float PrevValue;
private float updatedValue;
public void Process()
{
float value = updatedValue;
obj.transform.Translate(0f, value - PrevValue, 0f);
PrevValue = value;
}
Even when value - PrevValue == 0, so obj.transform.Translate(0f, 0f, 0f) obj.transform.localPosition and obj.transform.position change value at each frame. This behavior result in something like this after some seconds :
instead of this one :
With obj.transform.Translate(0f, 0f, 0f) I get this after some seconds :
The was original position was this
What am I doing wrong? Knowing that i don’t make any action on the gameobject anywhere else and when I comment this code the object doesn’t move at all (normal)!
I get that behavior on all GameObject on which I apply translation that way.
Thanks!!!
Floating point error may accumulate from repeated operations. Two objects moved by identical amounts will not always remain the same relative distance from each other.
One way to enforce it is to parent the two objects to a common object, and then only set the local position of the sub-objects.
In this case it looks like you’re making a thrust reverser for a turbojet. Just slide its local position (relative to perhaps the wing or the engine pylon) out and back along the +Z axis as the reverser deploys / stows.
Thanks @Kurt-Dekker for your reply,
I thought about parenting the objects and change the localPosition along a specific axis. Unfortunately, there are several aircrafts 3D models and several part to translate per model. So it would be more interesting to use them as they are and avoid adding empty gameobject each time I have new part to translate or a new 3D model to manage.
I’ll try it on this object and may be apply it later on all models if I don’t find a workaround else
@Kurt-Dekker , as we discussed, I switch to moving object in local space. Actually, the gameobject I was trying to translate is already made as parent of the real reverser mesh by the 3D artist. Thus I just move its child (the real reverser mesh) for certain amount in local space (modifying its localPosition). I extended this method to all my translations (even for object with several children) that have the same structure. Here is my code :
public static partial class TransformExtension
{
public static void XMoveChildren(this Transform rootTransform, float moveAmount)
{
foreach (Transform t in rootTransform)
t.localPosition = new Vector3(t.localPosition.x + moveAmount, t.localPosition.y, t.localPosition.z);
}
public static void YMoveChildren(this Transform rootTransform, float moveAmount)
{
foreach (Transform t in rootTransform)
t.localPosition = new Vector3(t.localPosition.x, t.localPosition.y + moveAmount, t.localPosition.z);
}
public static void ZMoveChildren(this Transform rootTransform, float moveAmount)
{
foreach (Transform t in rootTransform)
t.localPosition = new Vector3(t.localPosition.x, t.localPosition.y, t.localPosition.z + moveAmount);
}
}
Though this seems working, it looks a bit dirty and it would be nice if anyone find a proper workaround about the original translate (avoiding move on other axes than the one we wish to move along).