x


transform.Translate strange results with children

Hello,

I have my player GameObject, towing a chain of other objects using SpringJoints, all works well & good. They are flying in a pseudo-2D world, which is a finite size. When the player goes past a certain limit near the edges, I translate everything across to the other side, so it's like having a globe world - again, this works fine for the player. Trouble is, the towed objects don't seem to translate properly, and spend a few frames whizzing about as their spring joints readjust, so it looks a bit odd. The basic code is -

Player (on FixedUpdate() ):

float posX = transform.position.x;
float posZ = transform.position.z;

bool hasCircumnavigated = false;
Vector3 shift = Vector3.zero;

if (posX < worldXLimitLeft) {

    shift = new Vector3(worldWidth, 0f, 0f);
    hasCircumnavigated = true;

} else if (posX > worldXLimitRight) {

    shift = new Vector3(-worldWidth, 0f, 0f);
    hasCircumnavigated = true;

}

if (posZ < worldZLimitBack) {

    shift = new Vector3(0f, 0f, worldDepth);
    hasCircumnavigated = true;

} else if (posZ > worldZLimitFront) {

    shift = new Vector3(0f, 0f, -worldDepth);
    hasCircumnavigated = true;

}


if (hasCircumnavigated) {

    transform.Translate(shift);

    trailingObjectsManagerScript.RelocateTrailingObjects(shift);

}

Trailing objects:

public void RelocateTrailingObjects(Vector3 theMovement) {

    foreach (GameObject theObject in towedObjects) {
       theObject.transform.Translate(theMovement);
    }

}

It's as if the spring joint is affecting the translation somehow. If anyone knows what's going on, I'd appreciate some advice!

Thanks

more ▼

asked Feb 01 '12 at 11:59 AM

franklynw gravatar image

franklynw
81 2 4 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, I fixed it, but to be honest, I don't know why the first method didn't work. The fix is -

public void RelocateTrailingObjects(Vector3 theMovement) {

    foreach (GameObject theObject in towedObjects) {

       Vector3 objectPos = theObject.transform.position;
       objectPos += theMovement;

       theObject.transform.position = objectPos;

    }

}

If anyone can follow this up, I'd be very happy!

more ▼

answered Feb 02 '12 at 03:59 PM

franklynw gravatar image

franklynw
81 2 4 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x223
x11

asked: Feb 01 '12 at 11:59 AM

Seen: 361 times

Last Updated: Feb 02 '12 at 03:59 PM