• 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
1
Question by GrumpyZapdos · Nov 30, 2014 at 10:44 PM · rotationpositionrotaterotate objectaircraft

Rotate an object back to its old rotation

Hello all.May be the title is not so clear,so I would like to explain it.I am currently making an aircraft game.The controls are simple,press arrow buttons and rotate it around z axis.I managed to do this.The problem is that,I want the aircraft to return its idle position smoothly if no arrow keys are pressed.I tried some code but it didn't work.I really appreciate any help.Thank you!

Here my code to turn it:

 using UnityEngine;
 using System.Collections;
 
 public class CarController : MonoBehaviour {
 
     private float sensitivity = 70;
     private float speed = 40;
     public float zRotate;
     public float backReturn;
     private bool rightArrowUp = false;
 
     void Update () {

         // change position by arrow keys    
         transform.position += new Vector3(Input.GetAxis("Horizontal")*speed*Time.deltaTime,0,0);

         // change rotation by arrow keys    
         zRotate -= Input.GetAxis("Horizontal")*sensitivity*Time.deltaTime;    
         zRotate = Mathf.Clamp(zRotate, -5, 5);
         transform.rotation = Quaternion.Euler(0, 0, zRotate);

        //rotate back its idle position(0 degrees)
        // how????
     
     }
       
 }
 


Comment
Add comment · Show 2
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 bubzy · Nov 30, 2014 at 10:59 PM 0
Share

this really is a simple question, perhaps you should spend some time in the unity coding manual....

http://docs.unity3d.com/ScriptReference/Transform-rotation.html

http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html

its much quicker to check the manual and you learn quicker if you find the solution for yourself :)

avatar image GrumpyZapdos · Nov 30, 2014 at 11:03 PM 0
Share

thank you for reply.But actually,I would like to find out the algoirthm.I mean,I want an automatic rotation algorithm to the initial rotation when I stop pressing the arrow keys.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by bubzy · Nov 30, 2014 at 11:12 PM

just wrote this.

Lerp is what you need though, great if you can work this out for yourself, so consider this code a spoiler that you dont have to look at

 using UnityEngine;
 using System.Collections;
 
 public class rotateThis : MonoBehaviour {
 
     // Use this for initialization
     Quaternion originalRotation;
     float rotateSpeed = 0.1f;
     bool restoreRotation = false;
 
     void Start () {
         originalRotation = transform.rotation;
     }
     
     // Update is called once per frame
     void Update () {
     
 
         if(Input.GetKeyDown(KeyCode.A)&& !restoreRotation)
         {
             restoreRotation = true;
         }
 
         if(restoreRotation)
         {
             transform.rotation = Quaternion.Lerp(transform.rotation,originalRotation,Time.time * rotateSpeed);
             if(transform.rotation == originalRotation)
             {
                 restoreRotation = false;
             }
         }
     }
 }
 
Comment
Add comment · Show 2 · 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 GrumpyZapdos · Nov 30, 2014 at 11:24 PM 0
Share

Hey thank you! I modified it for my purpose but it doesn't work.The code is below:

 using UnityEngine;
 using System.Collections;
 
 public class CarController : $$anonymous$$onoBehaviour {
 
     private float sensitivity = 70;
     private float speed = 40;
     public float angle;
     public float rotAngle;
     public float zRotate;
     public float backReturn;
 
     Quaternion originalRotation;
     float rotateSpeed = 0.1f;
     bool restoreRotation = false;
 
     void Start(){
         originalRotation = transform.rotation;
     }
 
     void Update () {
 
         transform.position += new Vector3(Input.GetAxis("Horizontal")*speed*Time.deltaTime,0,0);
         angle = transform.eulerAngles.z;
 
         if( ( Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.RightArrow) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftArrow) ) && !restoreRotation)
         {
             restoreRotation = true;
         }
 
         if(restoreRotation)
         {
             transform.rotation = Quaternion.Lerp(transform.rotation,originalRotation,Time.time * rotateSpeed);
             if(transform.rotation == originalRotation)
             {
                 restoreRotation = false;
             }
         }
 
         if(!restoreRotation){
             zRotate -= Input.GetAxis("Horizontal")*sensitivity*Time.deltaTime;    
             zRotate = $$anonymous$$athf.Clamp(zRotate, -5, 5);
             transform.rotation = Quaternion.Euler(0, 0, zRotate);
 
         }
 
     }
 
 
 
 }
 

 
avatar image bubzy · Dec 04, 2014 at 03:04 PM 0
Share

try

else if(!restoreRotation){

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

How do I rotate an object when I press down a key and then it rotates back when I release the key 0 Answers

How to turn a car like in real world? 1 Answer

fixed rotation around object 1 Answer

Strange rotation pattern. 0 Answers


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