# Object keeps rotating to starting rotation..?

Hi guise,

My game is 2D, and I have this script to rotate objects(in this case, the player) on the Y axis depending on horizontal input:

``````var smooth = 2.0;
var tiltAngle = 30.0;

function Update () {
var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;

var target = Quaternion.Euler (0, tiltAroundY, 0);
// Dampen towards the target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, target,
Time.deltaTime * smooth);;
}
``````

When I use the code, the object rotates to desired rotation, but goes back when the horizontal axis is released…does anyone know what I should do to KEEP the object (player) at the rotation even after the horizontal axis is released?

``````var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
``````

Will always become 0 when you let go of the joystick or whatever it is you’re using. You should change it to this:

``````var tiltAroundY = transform.rotation.y + Input.GetAxis("Horizontal") * tiltAngle;
``````

So that you modify the current rotation. If you want it to be stuck to only 2 specific angles, then use this:

``````var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
if(tiltAroundY == 0) tiltAroundY = transform.rotation.y;
``````

I think you might also be misunderstanding something to do with either the Slerp function or Time.deltaTime, as well, since you’re using Time.deltaTime as the factor in your slerp. Time.deltaTime is the amount of time between the current frame and the last, so it’ll usually be in the vicinity of 0.03 (assuming your game is running at 30fps). When slerping, if you pass in a factor of 0 it returns the starting value, if you pass in 1 it gives you the target value, and values in between will give you something interpolated between the two. Unless I’m misreading your code, you’re always passing in something near 0, so it’ll always give you back a value near your starting value rather than your target value.

Thanks for your responses. Yes, I do want the object to be stuck to only 2 angles (with the rotation between them smoothed out), but I couldn’t seem to get your codes to work…
With a bit of tinkering, I came up with this:

``````var smooth = 4.0;
var tiltAngle = 90.0;

function Update () {
if (Input.GetAxisRaw("Horizontal")) {
var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle; //it will be 1 x 90 or 0 x 90.

var target = Quaternion.Euler (0, tiltAroundY, 0);
// Dampen towards the target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, target,
Time.deltaTime * smooth);;
}
}
``````

The result are pretty much what I wanted; it stops calculating the rotation when the user releases the axis, leaving it at it’s rotation. The only real problem is that when I tap the horizontal button, it still tries to calculate the returning rotation.