# Getting an object to rotate back to zero after key is released

Quick question, I’m trying to get my object to rotate back to zero (Level out) after the Horizontal mapped default keys have been released, I’ve looked around the forums but nothing seems to be working.

Here is the code so far

``````#pragma strict

var minimumX : float = -360;
var maximumX : float = 360;

var minimumY : float = -60;
var maximumY : float = 60;

var rotationX : float = 0;
var rotationY : float = 0;

var RSpeed : float = 100;
var dampSpeed : float = 0.5;

var originalRotation : Quaternion;

function Update () {

rotationX += Input.GetKeyDown("Horizontal") * Time.deltaTime * RSpeed;
rotationY += Input.GetAxis("Vertical") * Time.deltaTime * RSpeed;

rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);

var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.left);
var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.up);

transform.rotation = originalRotation * xQuaternion * yQuaternion;

if (Input.GetKeyUp("w"))
{
transform.localRotation.x = Quaternion.Slerp (transform.localRotation.x, originalRotation, RSpeed * Time.deltaTime);
}
if (Input.GetKeyUp("s"))
{
transform.localEulerAngles.x = Mathf.SmoothDamp (transform.localEulerAngles.x, 0.0, dampSpeed, RSpeed);
}
}

function Start ()

{
originalRotation = transform.localRotation;
}

static function ClampAngle (angle : float, min : float, max : float) : float

{
if (angle < -360.0)
angle += 360.0;
if (angle > 360.0)
angle -= 360.0;
return Mathf.Clamp (angle, min, max);
}
``````

Any ideas?

Thanks

The issue is that you are trying to Slerp the rotation in the Input.GetKeyUp, Slerp is a method that translates the object over time. Input.GetKeyUp only fires once. There are many different ways you could approach this and I’ll give some basic code below, you’ll need to work out the details:

``````//Set a boolean value on GetKeyUp and pool that boolean value.

var resetRotation:Boolean = true;

void Update()
{
if(Input.GetKeyUp("w"))
{
resetRotation = true;
}

if(resetRotation)
{
//put you Slerp in here and change resetRotation to false after it has reached reset position
}else{
//put you mouse interaction here
}

}
``````

First you are mixing transform.localRotation and transform.rotation. In general, you should pick one or the other to deal with. Next in order to make your code work, you need to either make it modal (i.e. it tries to rotate to the original rotation while the key is down and otherwise uses GetAxis()), or you have to use the same mechanism for rotating both using GetAxis() and when you want to rotate back to the original rotation. Here is a mod of your code that will bring the object back to the original when a key is pressed by using the same mechanism for both:

``````var minimumX : float = -360;
var maximumX : float = 360;

var minimumY : float = -60;
var maximumY : float = 60;

var rotationX : float = 0;
var rotationY : float = 0;

var RSpeed : float = 20;
var dampSpeed : float = 0.5;

var originalRotation : Quaternion;
var qTo : Quaternion;

function Update () {

rotationX += Input.GetAxis("Horizontal") * Time.deltaTime * RSpeed;
rotationY += Input.GetAxis("Vertical") * Time.deltaTime * RSpeed;

rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);

var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.left);
var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.up);

if (Input.GetKeyUp("w"))
{
rotationX = 0.0;
rotationY = 0.0;
}

qTo =  originalRotation * xQuaternion * yQuaternion;

transform.rotation = Quaternion.Slerp(transform.rotation, qTo, RSpeed * Time.deltaTime);
}

function Start ()
{
originalRotation = transform.rotation;
qTo = transform.rotation;
}

static function ClampAngle (angle : float, min : float, max : float) : float

{
if (angle < -360.0)
angle += 360.0;
if (angle > 360.0)
angle -= 360.0;
return Mathf.Clamp (angle, min, max);
}
``````