- Home /
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;
 }
 
  
 
 
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;
     }
  }
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;
 }
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
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                