How to Rotate an Object based on the Difference of the Cameras Last Rotation

Hi all,

I have a Trolley in my game which Rotates around the player (GIF Below for Example) I am trying to make it so the Trolley doesn’t ‘Skid’ sideways when the player moves the Camera.


GIF Example:


I attempted to use Animation Events to achieve this though it is a little slow and sometimes doesn’t work as shown below.


GIF Example:


I am wondering if there is a way that this can be done through C# to make it more fluent and making it so the Tolley’s force toward the rotating direction is increased as the rotation (difference) is greater, when the Camera Rotation slows the rotation force of the Trolley eases off, when there is little to no Camera Movement it should be returned to normal forward facing direction.


This is the code currently used to handle the Trolleys rotation (with the Update function):

camRotY = camRot.eulerAngles.y;
            if (trollieRot.eulerAngles.y != camRotY)
            {
                float diff = 0;
                Debug.Log("Camera Rot Y is: " + camRotY);
                diff = Math.Abs(camRotY - lastYRot);
                if (diff < 1)
                {
                    anim.SetBool("left", false);
                    anim.SetBool("right", false);
                    anim.SetBool("idle", true);
                }
                else
                {
                    if (lastYRot < camRotY)
                    {
                        anim.SetBool("left", false);
                        anim.SetBool("right", true);
                        anim.SetBool("idle", false);
                    }
                    else if (lastYRot > camRotY)
                    {
                        anim.SetBool("left", true);
                        anim.SetBool("right", false);
                        anim.SetBool("idle", false);
                    }
                }
                trollieRot.eulerAngles = new Vector3(
                    trollieRot.eulerAngles.x,
                    camRotY,
                    trollieRot.eulerAngles.z
                );
                lastYRot = camRotY;
            }

For Example something like this but more dynamic that the rotation is basically directly affected by the current difference:

if(diff == 0.1){
//rotate a little bit
}
if(diff == 0.5){
//increase rotation angle
}
if(diff => 1){
//use max roational angle
}

I was able to Get it working successfully using the below code:

camRotY = camRot.eulerAngles.y;
            trolleyRotY = trolleyRot.eulerAngles.y;
            if (trolleyRotY != camRotY)
            {
                diff = Math.Abs(camRotY - lastYRot);
                tDiff = Math.Abs(trolleyRotY - camRotY);
                Debug.Log("Camera Rot Y is: " + camRotY.ToString("f3") + " and Difference is: " + diff.ToString("f3") + " Trolley Difference is: " + tDiff.ToString("f3"));

                trolleyRot.eulerAngles = new Vector3(
                    trolleyRot.eulerAngles.x,
                    camRotY,
                    trolleyRot.eulerAngles.z
                );

                if (diff < 0.1f && trolleyTrans.localRotation != new Quaternion(operatingAngle.x, 0, 0, 0))
                {
                    float iR = elapsed / speed;
                    trolleyTrans.localRotation = Quaternion.Lerp(trolleyTrans.localRotation, new Quaternion(operatingAngle.x, 0, 0,0), iR);
                    elapsed++;
                }
                else if (diff < 0.1f && trolleyTrans.localRotation == new Quaternion(operatingAngle.x, 0, 0, 0) && elapsed > 0)
                {
                    elapsed = 0;
                }
                else if (diff > 0.1f)
                {
                    if(diff < 90)
                    {
                        if (lastYRot < camRotY)
                        {
                            trolleyTrans.localEulerAngles = new Vector3(operatingAngle.x, diff * 3, 0);
                        }
                        else if (lastYRot > camRotY)
                        {
                            trolleyTrans.localEulerAngles = new Vector3(operatingAngle.x, -diff * 3, 0);
                        }
                    }
                }

                lastYRot = camRotY;
                lastTYRot = trolleyRotY;

                

            }

GIF Example:


Still a little Jank, but certainly a lot better than before and with some improvements hopefully can get it looking a bit smoother.