Maintain direction regardless of rotation

Hey there! So, I am attempting to control my player on the X axis, with the left and right keys, which works thus far. I am then trying to layer on some rotational functionality, whereupon the player character ‘leans’ left or right dependant on movement. I have also been able to achieve this.

However, my issue arises when the player character successfully leans in to its movement but then subsequently moves in that new direction. See figure for example.

The blue square represents my player character at a standstill. While the red square represents what currently happens when it leans in to its movement. Lastly the green square represents how I would like it to act.


Here is my code thus far.

#pragma strict

var playerSpeed = 10.0;
var playerRotation = -5;

function Start () {}

function Update () 
 var movementX = Input.GetAxis("Horizontal") * Time.deltaTime * playerSpeed; 
 var rotationZ = Input.GetAxis("Horizontal") * playerRotation;
 transform.Translate(movementX, 0, 0); //dictates the movementX variables direction. In this case, obviously the X axis.
 transform.localRotation = Quaternion.Euler(0,0,rotationZ);

Use Space.World in Translate. (Another possibility: make an empty object that’s used as the transform which is controlled by the player, with the visible object as a child of the empty object. The child would be rotated but not the parent.)