Rotating twice during the same frame

The following code works as expected:

if (hit)
{
    dir = normal;
    Vector3 playerPos = RigidbodyController.Instance.Transform.position;
    Quaternion normalRotation = Quaternion.LookRotation(normal, Axis);

    float angle = Mathf.Atan2(playerPos.x - PrefabTransform.position.x, playerPos.z - PrefabTransform.position.z) * 180 / Mathf.PI;

// want to combine these into one quaternion. and set PrefabTransform.rotation  = finalRotation;
    PrefabTransform.rotation = normalRotation;
    PrefabTransform.rotation = PrefabTransform.rotation * Quaternion.Euler(new Vector3(0, 0, angle));

}
else
{
    PrefabTransform.rotation = lastRotation;

}

I am not exactly sure how to combine those two rotations into a quaternion. Anyone have any advice?

By “Combine” you mean applying one after another?
In this case just multiply them.

Maybe it could be easier for you to solve your problem using Euler angles (x, y, z like in the Editor) instead of dealing with Quaternions.

You can multiply (* operator) two quaternions to combine them. The order does matter here, though I can’t remember what side is what, but experimenting a bit should get you the results you want.

The second quaternion in a multiplication will be applied first.

1 Like

Appreciate the response. That’s what I am struggling with: the order. Wondering if anyone can push me in the right direction. Can’t seem to find the proper order for the final rotation. I’ve tried many combos already and was wondering if I’m going about this wrong.

        if (hasItem)
        {
            Vector3 normal = Vector3.zero;
            //Quaternion rotation = Quaternion.identity;
            Vector3 hitPoint = Vector3.zero;
            bool hit = false;
            SetItem(ref hitPoint, ref normal, ref hit);
          
            PrefabTransform.position = Vector3.SmoothDamp(PrefabTransform.position, gotoPosition, ref posVel, PosDampening);
            Vector3 dir = -camTransform.forward;

            if (hit)
            {
                dir = normal;
                Vector3 playerPos = RigidbodyController.Instance.Transform.position;
                Quaternion normalRotation = Quaternion.LookRotation(normal, Axis);

                float angle = Mathf.Atan2(playerPos.x - PrefabTransform.position.x, playerPos.z - PrefabTransform.position.z) * 180 / Mathf.PI;
                /*                PrefabTransform.rotation = normalRotation;
                                PrefabTransform.rotation = PrefabTransform.rotation * Quaternion.Euler(new Vector3(0, 0, angle)) * Quaternion.Euler(new Vector3(0, 0 , AddedRotation));*/

                // doesnt work. goes crazy
                //Quaternion finalRotation = normalRotation * PrefabTransform.rotation * Quaternion.Euler(new Vector3(0, 0, angle)) * Quaternion.Euler(new Vector3(0, 0, AddedRotation));

                // doesnt work. goes crazy
                //Quaternion finalRotation = PrefabTransform.rotation * normalRotation * PrefabTransform.rotation * Quaternion.Euler(new Vector3(0, 0, angle)) * Quaternion.Euler(new Vector3(0, 0, AddedRotation));

                // doesnt work. goes crazy
                Quaternion finalRotation = Quaternion.Euler(new Vector3(0, 0, angle)) * Quaternion.Euler(new Vector3(0, 0, AddedRotation)) * PrefabTransform.rotation * normalRotation * PrefabTransform.rotation;

                PrefabTransform.rotation = finalRotation;

            }

What you’re trying to achieve exactly?

This script places the object at a rays hit.point. Then lines it up properly with the rays normal. I add an additional rotation based on an angle to point the object in a certain direction. It all works.

I am looking for a final quaternion that combines those double rotations so I can smoothly rotate the initial rotation to the final rotation.

Would also avoid doing super long sequences of calculations like that. Do one at a time; step by step, giving each local variable a descriptive name to help you understand what everything done.