Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 paco morales · Oct 07, 2015 at 05:41 PM · androidjavascriptrotate

Return to the origin of rotation

Hi,

I am trying to modify a script I found, This script is for rotate an object and works well, but I want to add the function of return to the origin of rotation after 2 seconds of inactivity.

Please any advice is more than welcome!

Thanks

 // Moves object according to finger movement on the screen
 
 var speed : float = 0.1;
 var rotationStart = 0.0;
 var rotatehome = false;
 var rotationSpeed = 10;
 
 
 function Awake ()
 {
     transform.Rotate(rotationStart,0,0);
 }
  
 function Update () {
     
     if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) {
         
         // Get movement of the finger since last frame 
         
     var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
 
         // Move object across XY plane 
     
     transform.Rotate (0,-touchDeltaPosition.x * speed, 0);
  
     rotatehome = false;    
     }
      else if (rotatehome)
    {
     transform.rotation = Quaternion.RotateTowards(transform.rotation, new Quaternion(0,0,0,0.1), rotationSpeed);
     }
     }  
 
    function Return()
 {
     yield WaitForSeconds (0.5);
     rotatehome = true;
 }
 
  
 
 
Comment
Add comment
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

1 Reply

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

Answer by JA_555 · Oct 07, 2015 at 11:33 PM

  var rotationSpeed = 10;
  var rotationStart = 0.0;
  var rotatehome = false;
  var timer : float =0;
  var fingeroff : boolean;
  function Awake ()
  {
      transform.Rotate(rotationStart,0,0);
  }
  
   
  function Update () {
           
      if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) {
   
          // Get movement of the finger since last frame 
           
          var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
  
          transform.eulerAngles.y = (transform.eulerAngles.y - Input.GetAxis("Mouse X") * rotationSpeed);
  
          timer = 0;
  
          rotatehome = false;
 
     fingeroff = false;
      
     }else{
 
      if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended)
     {
     fingeroff = true;
     }
     }
     
  
      if (rotatehome)
      {         
          transform.rotation = Quaternion.RotateTowards(transform.rotation, new Quaternion(0,0,0,0.1), rotationSpeed);
      }
       
  }
  function FixedUpdate()
  {
      if (fingeroff ==true){
      timer += 1 * Time.deltaTime;
      }
      
      if (timer >= 5){
     rotatehome = true;
     }
  }


Comment
Add comment · Show 3 · 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 paco morales · Oct 07, 2015 at 11:43 PM 0
Share

Hi JA_555,

I made this correction but I don't know why the 5 seconds of pause or delay I put doesn't work. Any Idea? Thanks for your replay!

 var rotationSpeed = 10;
 var rotationStart = 0.0;
 var rotatehome = false;
  
 function Awake ()
 {
     transform.Rotate(rotationStart,0,0);
 }
 
  
 function Update () {
          
     if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
  
         // Get movement of the finger since last frame 
          
         var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
 
         transform.eulerAngles.y = (transform.eulerAngles.y - Input.GetAxis("$$anonymous$$ouse X") * rotationSpeed);
 
         Return();
 
         rotatehome = false;
     }
 
     else if (rotatehome)
     {         
         transform.rotation = Quaternion.RotateTowards(transform.rotation, new Quaternion(0,0,0,0.1), rotationSpeed);
     }
      
 }
 function Return()
 {
     yield WaitForSeconds (5.0);
     rotatehome = true;
 }
avatar image JA_555 paco morales · Oct 07, 2015 at 11:46 PM 0
Share

I might be wrong but I think its because it's being called in update so it is being called every frame. You could take that part out and have a FixedUpdate function that says if there is no touch, set a rotation.

This means you will have a regular update calculating your touch movements, and a fixedupdate function that does things when there are no movements based on a timer

I edited the script in the answer, see how it works out

avatar image paco morales JA_555 · Oct 08, 2015 at 12:47 AM 0
Share

Thanks $$anonymous$$an, works very well!

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

38 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

'GetComponent' is not a member of 'Object'. 2 Answers

Is there a way to scale everything in my game (GUI, text size, etc.) on Android in Javascript? 2 Answers

How to email highscore through android? 2 Answers

pong rolling ball 1 Answer

Object Touch Problem 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