Rotate an object to match another

Lots of answers already, but I can’t find a formula that works for my scenario.

I have 2 objects, with the same parent structure. I want Object B to rotate over time to match the orientation of Object A, using the shortest path. This will be around the y axis with a little bit of movement on the x axis.

157957-rotate.jpg

Their relative positions to one another will stay the same, although their parent is moving all of the time. I hope local Rotation removes this part of the problem.

How do I rotate one towards the other? I’ve reviewed RotateTowards, Lerping and Slerping, and I’m happy to use whatever is the best method. I’ve tried to implement these, but I’m missing some basic knowledge because the rotations are in the wrong direction, or they go the long way around, or around the wrong axis.

I think this is what you need:

[157963-test-min.gif|157963]

    public Transform target;
    public float speed;
    
    void Update()
    {
        transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, speed * Time.deltaTime);
    }