changing object rotation to be like another object

hello every one i’m having a problem with

transform.rotation.y = target.rotation.y;

when i but this code in a cube it doesn’t rotate like the target.
when i rotate the target it follow the rotation but not exactly the same because the cube doesn’t rotate a full rotation.

if anyone try this will know what i mean.

so if there any rotation code that i can use i would like to know about it.

sorry if my English is bad.

and thanks for any help.

Then, you have to create a rotation yourself with the x and z values from the first object and the y value from the other. Check the docs here. It should be something like this:

Vector3 eulerRotation = new Vector3(transform.eulerAngles.x, otherObject.transform.eulerAngles.y, transform.eulerAngles.z);

transform.rotation = Quaternion.Euler(eulerRotation);

Although there’s another way of rotating your object at the same time as the other object, which is modifying the transform.forward vector of your object. You have to create a Vector3 with x and z components from otherObject.transform.forward and set the y component to the y component of your own transform.forward. Something like this:

transform.forward = new Vector3(otherObject.transform.forward.x, transform.forward.y, otherObject.transform.forward.z);

You may need to Normalize it.

You can’t set just one component of the rotation. You need to set the whole rotation.

i tried this too but it didnt worked

transform.rotation = Quaternion.Euler(transform.rotation.x, cam.rotation.y, transform.rotation.z);