PingPong on local axis. Or not, as the case may be.

Hi,

I have 5 gameobjects that are arranged in a circle. Each gameobject is rotated in the world-z by 72 degrees so that they all orient towards the center of the circle (like the markings on a clock face).
See Image

I want those items to converge on the center of the circle, and then move back out again.
I really thought this would be simple, but have been unable to get it to work trying various methods. This is my latest failure. This script sits on an empty GameObject called PillarController.

    public List<GameObject> Pillars = new List<GameObject>();
    public float distance;
    public float MoveSpeed;

    void Update()
    {
        foreach (var pillar in Pillars)
        {
            var localPos = pillar.transform.localPosition;
            var newY = Mathf.PingPong(MoveSpeed * Time.time, distance) - (distance / 2);
            Debug.Log(newY);
            pillar.transform.Translate(localPos.x, newY,localPos.z, Space.Self);
        }
    }

Each capsule is parented to an empty game object that is at (0,0,0). The capsules are then offset on their Y axis (by 2 units in this case). The parent game objects are rotated in their z-axis by 72 degrees.
See Image

When this runs, a few of the capsules ping-pong in the global Y axis, and a couple just find it all too much and shoot off into space, never to be seen again, which quite frankly seems like a good idea at the moment.

What the flip am I doing wrong? Example attached as package.

Thanks
Mick

8121416–1052642–PingPong.unitypackage (9.62 KB)

transform.localPosition does not account for the rotation of the object itself, only the rotation of the parent. When translating the pillars, the X and Z values should be 0, as Translate moves it based on its current position, and you don’t want it to move in the X and Z axis.

2 Likes

If you ever start a religion, sign me up as your top disciple!

Thanks, that has resolved the issue.

Note the easiest solution for things like that is to use a seperate parent object for each of your pillars. Have those parents all located in the center. You would simply rotate those parent objects the way the individual pillars should face. That way all child objects behave the same locally inside their own parents. So you simply move the local position up and down along a single local axis inside the parent. So the parent defines the orientation in the world.

Each pillar has a parent. Each parent is at (0,0,0). Each parent is rotated 72 degrees. However, I am then moving the parent, and not the pillar. I’ll give it a go.