Does transform.LookAt() override z rotation?

I’m currently trying to make almost a snake like simulation where a number of cubes are following each other with slight delays. Everything was working as well as the mimicking of the Z rotation of the target causing a spiral until I added transform.LookAt(target). Now the subsequent blocks do not turn in z axis no matter what the target does. here is a bit of the code, excuse the huge mess. :slight_smile:

Quaternion currentRotation = transform.rotation;
        Quaternion targetRotationWithoutY = Quaternion.Euler(currentRotation.eulerAngles.x, targetRotation.eulerAngles.y, targetRotation.eulerAngles.z);
        Quaternion targetRotationWithoutYZ = Quaternion.Euler(targetRotationWithoutY.eulerAngles.x, 0f, 0f);
        desiredRotation = Quaternion.Lerp(currentRotation, targetRotationWithoutYZ, rotationSpeed * Time.deltaTime);

        //this makes the objects rotate in the z axis
        float desiredZRotation = Mathf.LerpAngle(currentRotation.eulerAngles.z, targetRotationWithoutY.eulerAngles.z, spiralSpeed * Time.deltaTime);
        desiredRotation.eulerAngles = new Vector3(desiredRotation.eulerAngles.x, desiredRotation.eulerAngles.y, desiredZRotation);

        transform.rotation = desiredRotation;

        // Look at the target
        transform.LookAt(target.transform.position);
    }

I’ve messed with the spiral speed but that doesn’t make a difference.

There’s a solution for the huge mess!!

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

The magic you’re looking for is the optional second argument to the Transform.LookAt() method.

Hie thee to the Transform documentation to see what I mean!