Object moving backward when moving forward

So I’m rotating an object along y axis so that it rotates towards where the camera is facing.
And then translating along x and z according to the axis. It moves perfectly but a lot of times it starts moving backwards when pressing forward button and left when pressing right button.
This generally happens when it’s rotated 180 then it happens until rotated to front.

This is my code. Any other way to set object rotation to camera rotation?

   Quaternion newRotation = transform.rotation;
    newRotation. y = cam.rotation.y;
    transform. rotation = newRotation;
    
    transform.Translate(speed * Input. GetAxis("Horizontal"), 0, speed * Input. GetAxis("Vertical")) ;

Good day.

If you amke the rotation to be the same as the camera, you can not change that, if the camera is looking at “more than 180º” the object also, so you cna not change that.

What you can change is the moving code.

You are using transform.translate, wich uses the local coordinates of the object, (not global wolrd coord) so, if the object is facing left and it goes “Positive Vertical Axis” it will move to left.

You need to move the obejct with changing its transform.position (which uses Global world corrds), for example in a corrutine, or inside the update.

You have several Youtube tutorials, manuals and Unity Answers pages about how to do it!

Good luck!