How to make a Separate Object follow the Cameras Y Rotation?

Hi all,
This is an issue I am forever facing while developing with unity is that Object/Transform Rotations via scripting is so darn confusing and complicated.


I am attempting to add a Trollie to my game and am struggling to have it (while interacting) follow the facing direction of the camera, I finally managed to get the Trollie to rotate via the script, though the values being read and applied from the cameras Transform are from -1 to +1 in value (and not -180 to +180 like you would think), which when this is applied to the Trollie it only rotates microscopically as a full ‘spin’ in regards to rotation is 360° not 1°… To attempt counter this I tried to multiply the generated value by 100, though after doing this when going past 180° of the camera rotation the Trollie would start spinning the opposite direction than the camera…

Long story short, how does one crack divinici’s code to FORCE the Trollie Transform to be EXACTLY IDENTICAL to what ever the Camera’s Y Rotation is? To me it is just so confusing that entering a value of say 90 into the Transform Rotation in the Inspector translates to 0.5 once the script gets a hold of the value, why is it that somewhere this 90° is being converted to 0.5 and how can this be avoided to actually allow 2 separate objects follow another’s rotation ‘EXACTLY’?

Better yet, rather than the script grabbing a value that doesn’t seem visible anywhere within the inspector, how can I make the script grab the actual value that is being displayed in the inspector rather than a value of 1 being equal to 180 and -1 being equal to -180.


Due to the fact that I am absolutely atrocious at trying to explain myself and always fail at trying to portray what I mean, I have created a video that demonstrates exactly what is going on and I hope it clears up what I mean about that the script is telling the TrollieRot to be equal to the CameraRot though the value being grabbed via the CameraRot, only moves the TrollieRot microscopically and in no way what so ever does it match the cameras actual Y Rotation Value.



CODE BEING USED:

void Update()
    {
        if(isInteractingWithTrollie)
        {
            trollieRot.rotation = Quaternion.Euler(new Vector3(0,camRot.rotation.y,0));
            Debug.Log("Rotating Trollie to Suit Camera Rotation, Trollie Rot is: "+trollieRot.rotation.y.ToString("F3")+", Camera Rot is: "+camRot.rotation.y.ToString("F1"));

            if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.A))
            {
                rotWheelsForward();
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                rotWheelsBackward();
            }
        }
    }

I have also attempted to remove the ‘new Vector3’ portion from the code and just use ‘trollieRot.rotation = Quaternion.Euler(0,camRot.rotation.y,0);’ though this results in the exact same outcome…

With the Debug Log Print out I am receiving this message:

“Rotating Trollie to Suit Camera Rotation, Trollie Rot is: 0.001, Camera Rot is: 0.1”

Which you can see that even though the script is attempting to force the trollieRot to be the exact same as the camRot (minus the X and Z Values ofc) it somehow and for some reason converts the value from triple digits (0-180) to a single digit (0-1) which is exactly what is causing the trollie to not follow the camera rotation as the values do NOT accurately represent the ‘ACTUAL’ rotational value.


Personally in my head a simple solution would be to do something as such:

trollieRot.rotation.y = camRot.rotation.y

however this would make it far too easy and simply does not work as individual values of a Quaternion cannot be adjusted like a variable, is there anyway that something can be done similar to the above? something that simply translates a value from one section and places it in another section without that value being automatically modified and adjusted to a completely different value than what is displayed/required, for example… camRot.y = -137.4 lets grab this -137.4 and apply it to the TrollieBase Object, rather than… lets grab this -137.4 and convert it to -0.32… more or less how can I grab the -137.4 value rather than the converted -0.32 value? or is there some sort of mathematical equation that can convert this molested value back to its original value?

So this is probably what you want:

            //If it is picked up get the direction between the pickup and the player
            var lookPos = cameraTransform.position - target.position;
            //This makes it so the pickup does not follow our up and down look rotation
            //You can always comment it out to make the object follow those rotations
            lookPos.y = 0;
            //Gets the direction of the object in a form that allows us to rotate
            var rotation = Quaternion.LookRotation(lookPos);
            //Rotates the pickup in our calculated look direction
            target.rotation = Quaternion.Slerp(target.rotation, rotation, Time.deltaTime * damping);
            //This moves the picked up object along with our position at a set distance away from 
            //the player
            Vector3 resultingPosition = cameraTransform.position + cameraTransform.forward * objectDistanceFromCamera;
            target.position = resultingPosition;