- Home /
 
Rotate on drag for IOS?
Hello all, I am new to Unity and JS so bear with me.. I'm trying to change a rotate script so that it will run on an IOS device, but having a little trouble. The script I have been using to rotate an object on my computer,
 var verticalSpeed : float = 2.0;
 var horozontalSpeed : float = 2.0;
 var depth : float = 2.0;
 
 function OnMouseDrag () {
     var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
     transform.Rotate ( -v, 0, 0);
     var h : float = horozontalSpeed * Input.GetAxis ("Mouse X");
     transform.Rotate (0, 0, h);
 }
 
               My attempt at the IOS script,
 var verticalSpeed : float = 2.0;
 var horozontalSpeed : float = 2.0;
 var mouseDown : boolean;
 
 function update (){
     if(Input.GetMouseButton(0)){
         mouseDown = true;
     }
     else{
         mouseDown = false;
     }
     if(mouseDown){
         drag();
     }
 }
 
 function drag () {
     var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
     transform.Rotate ( -v, 0, 0);
     var h : float = horozontalSpeed * Input.GetAxis ("Mouse X");
     transform.Rotate (0, 0, h);
 }
 
               Am I even close haha? Any ideas how I might be able to get this too work would be greatly appreciated. Thanks, Garrett.
I just formatted your code. Please do this in future by highlighting the code and pressing the 10101 button at the top-left =]
Answer by AlucardJay · Jul 23, 2012 at 07:29 PM
You need to look into the Input states for iOS, using touch is a bit different to mouse.
http://docs.unity3d.com/Documentation/ScriptReference/Input-touches.html
http://docs.unity3d.com/Documentation/ScriptReference/Touch.html
http://docs.unity3d.com/Documentation/ScriptReference/TouchPhase.html
(btw your Update is with a lower u)
To modify the Update for touch, something like :
 #pragma strict
 
 private var h : float;
 private var v : float;
 private var horozontalSpeed : float = 2.0;
 private var verticalSpeed : float = 2.0;
 
 function Update()
 {
     if (Input.touchCount == 1)
     {
         var touch : Touch = Input.GetTouch(0);
         
         if (touch.phase == TouchPhase.Moved)
         {
             h = horozontalSpeed * touch.deltaPosition.x ;
             transform.Rotate( 0, -h, 0, Space.World );
             
             v = verticalSpeed * touch.deltaPosition.y ;
             transform.Rotate( v, 0, 0, Space.World );
         }
     }
 }
 
               here's some sample script I posted in the forums : http://forum.unity3d.com/threads/142758-Moving-objects-with-your-finger
Thanks! It only seems to rotate around the Y axis though, any idea why?
I did change a few things in the code, sorry for the sloppy reply im still trying to figure out formatting.. **
private var h : float; private var v : float; var horozontalSpeed : float; var verticalSpeed : float;
function Update()
{ if (Input.touchCount == 1) { var touch : Touch = Input.GetTouch(0); if (touch.phase == TouchPhase.$$anonymous$$oved) {
         var v : float = verticalSpeed * touch.deltaPosition.x ;
         transform.Rotate ( h, 0, 0);
         
         var h : float = horozontalSpeed * touch.deltaPosition.y ;
         transform.Rotate (0, v, 0);
     }
 }
 
                  }
After some testing, I found that the object was rotating in Local Space, not World Space. http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
I have updated the answer, it is tested and working. I just hope this is what you were after!
is it possible to add scaling the object with a pinch (2 finger gesture)? if it is so it would be more than awesome..
Your answer