- Home /
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
I should mention as well that the parts where the key is released are two different approaches i've tried, but left in there so you could see what I have tried
you have line 19, set to a getkeydown ins$$anonymous$$d of a getaxis. if whatever your doing works on the vertical, you should probably change line 19 to read : rotationX += Input.GetAxis("Horizontal") Time.deltaTime RSpeed;
Oops, forgot that that used to say getaxis, i changed it to try it as getkeydown and forgot to change it back....bottom line is that it still doesn't work
Answer by robertbu · May 26, 2013 at 02:00 PM
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);
}
For some reason this didn't work, it still rotates to the clamp point when you press the key down, but when I release the key it just stays where it is and doesn't rotate back to zero, I also tried the previous answer, but all that did was just kept the transform at 0
I guess I don't understand what you want to do. What is the "clamp" point? This code will rotate back to the originalRotation when the 'w' key is lifted. It does nothing on key down. Start with a new scene, put a non-uniform mesh in the scene. Attach this script. Let me know how its behavior differs from what you want.
Ok, the clamp point is what the clamp function does, since Unity has no way of using $$anonymous$$athf.Clamp simply for clamping how far an object can rotate to, the Clamp function is there to help things along.
originalRotation is just set to whatever the transform.rotation is, would it help if I set that to 0.0? I know that the Start function should record that but I get the feeling that it isn't....
I'll try the new scene now in the meantime
Ok it worked, I figured out the problem, seems my meshes are the wrong way round and that's why it didn't seem to be working, I'll mark your answer as correct
Answer by kilgore · May 26, 2013 at 01:13 PM
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
}
}
Thanks, I prefer it as code to work from otherwise you never learn anything, but thanks for the heads up on the Get$$anonymous$$eyUp issue, I didn't realise that it only fired once.
Your answer
Follow this Question
Related Questions
transform.rotate only 1 time for 180 degrees? 1 Answer
making one object a parent of another via javascript 2 Answers
LookAt Problem 1 Answer
hoe to rotate a AI as a animation 1 Answer