Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Supershandy · May 26, 2013 at 12:55 PM · javascripttransformrotatemathfslerp

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Supershandy · May 26, 2013 at 12:56 PM 0
Share

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

avatar image Proportion1 · May 26, 2013 at 01:06 PM 0
Share

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;

avatar image Supershandy · May 26, 2013 at 01:11 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Supershandy · May 26, 2013 at 02:20 PM 0
Share

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

avatar image robertbu · May 26, 2013 at 02:39 PM 0
Share

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.

avatar image Supershandy · May 26, 2013 at 02:46 PM 0
Share

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

avatar image Supershandy · May 26, 2013 at 02:53 PM 0
Share

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

avatar image
1

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
 }
 
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Supershandy · May 26, 2013 at 01:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Javascript - Detect wall collision and flip 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges