# Maintain local transform.rotation.y when updating transform.up?

I am trying to rotate my character to match the ground normal, which is working, but it resets the character’s local rotation.y, and I understand that updating the transform.up of the character resets it, but how do I maintain the character’s y rotation? I’m using the following code to match the ground and rotate:

``````//Ground Match
RaycastHit hit;
if (Physics.Raycast (transform.position, -transform.up * 5, out hit)) {
if (transform.up != hit.normal) {
transform.up = hit.normal;
}

if (hit.distance < 1.25) {
transform.Translate (0, Time.deltaTime, 0);
}

}

/*Handle Core Rotation*/
if (h > 0 || h < 0) {
m = 0;
r += (h * handling) / 90;
} else if (h == 0) {
r = Mathf.Lerp(r, 0.5f, m);
m += 0.1f;
}

/*Limit Rotation*/
if (r > 1) {
r = 1;
} else if (r < 0) {
r = 0;
}
rotateConstant = Mathf.Lerp (-1, 1, r);

if (currentSpeed == 0) {
rotatePower = rotateConstant * ((handling / 5) * 4);
} else {
rotatePower = (rotateConstant * handling) * 2;
}

//Final Rotate
transform.Rotate(0, rotatePower, 0)
``````

The easiest way to do what you want is to use Quaternion.LookRotation().

Pass it your current forward direction and ground normal as up vector, it’ll return a rotation.

Something like this :

``````transform.rotation = Quaternion.LookRotation(transform.forward, hit.normal);
``````

Do you mean transform.localEulerAngles/transform.eulerAngles ? transform.rotation is Quaternion not angles. If you really want to have full control on y angle when dealling with ground normal, maybe you can use Quaternion.AngleAxis()