- Home /
Duplicate of
https://answers.unity.com/questions/1818019/how-do-i-set-the-y-rotation-of-one-object-to-anoth.html
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?
@Hellium was able to answer the question correctly on a separate post, though the post was unfortunately deleted, possibly due to duplicate post, though unfortunately once someone has attempted to answer the question, its highly unlikely others will attempt to answer, hence creating a new post.
@Hellium if you would like to add your answer to this post I will accept it as the solution to my problem, I cannot thank you enough for not only helping me with this problem, but also helping me a handful of times in the past!
Answer by KittenSnipes · Dec 05, 2021 at 12:23 AM
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;
Thank you for attempting to help, though I am not sure that this really relates to what I need, or at least I am unable to convert your 'Pickup Object' Script to my 'InteractWithTrollie' Script, I don't need any positioning movement as this is all handled by empty game objects that the Trollie becomes a child of once it has been interacted with, again I am horrible at trying to explain what I mean so I have made another video going into a bit more depth about how the system works and what is expected and what is actually happening.
That does help knowing a bit how your system works so I will try and emulate your system and hopefully fix what is going on. My code use the cameras look direction to rotate the object that is picked up which would be your trollie. So I will see what I can do I do want to help as much as I can. I will try and be back with an update soon.
Thank you so much for taking the time to help out, I greatly appreciate it :)
Down to a nutshell all I really need to happen is as follows: Grab Cameras Y Rotation, apply that Y Rotation to the TrollieBase Rotation, ie copy one value and apply it to another value.
Which you would think would be so simple yet, the way that the value gets automatically converted to from 180° to 1 and 90° would be 0.5 for example, which in my $$anonymous$$d is so misleading, confusing and undesirable when trying to apply a value that gets changed before I can even apply it's original value. I mean there has to be a simple way to get 1 Objects Rotational Value and apply that directly to another objects rotational value.
I just cannot fathom why....
trollieRot.rotation.y = camRot.rotation.y;
just doesn't work, as it seems so simple that it gets a value from one section and applies it to an identical section in a separate object, which works perfectly fine when working with positions and such, but when it comes to rotations it seems just so backwards, outdated and not user friendly what so ever.
I do apologise for the rant, I have been stuck with this problem for nearly a week now, trying sooo many different methods of rotation with the closest outcome being as seen in the first video toward the end where the trollie starts to follow the camera perfectly, but then after going past 180° the trollie then starts rotating the opposite direction than the camera, which I mean I could make a huge mess of code that checks these rotations and for example: if rot > 0 && < 180 then rotate clockwise, else rotate anti-clockwise, but why should it be so darn difficult to simply copy one value and apply it to another?
Follow this Question
Related Questions
Input.getaxis (and getaxisraw) not returning the correct values with my joysticks 0 Answers
What is wrong in my camera rotator script ? 0 Answers
Values not moving to Game Over screen or the Game over doesn't somehow work. 0 Answers
Creating an instance of a ScriptableObject, that will not change the base asset. 2 Answers
Mathf.Lerp doesn't work on light object's value change 4 Answers