• 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 GarrettH · Jul 23, 2012 at 07:13 PM · iosmousetouchrotatedrag

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.

Comment
Add comment · Show 1
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 AlucardJay · Jul 23, 2012 at 07:18 PM 0
Share

I just formatted your code. Please do this in future by highlighting the code and pressing the 10101 button at the top-left =]

1 Reply

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

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

Comment
Add comment · Show 5 · 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 GarrettH · Jul 24, 2012 at 06:20 PM 0
Share

Thanks! It only seems to rotate around the Y axis though, any idea why?

avatar image GarrettH · Jul 24, 2012 at 06:51 PM 0
Share

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);
     }
 }

}

avatar image AlucardJay · Jul 24, 2012 at 08:45 PM 0
Share

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!

avatar image GarrettH · Jul 25, 2012 at 02:00 AM 0
Share

Its working perfect ! Thanks a ton!

avatar image necipcik · Sep 04, 2014 at 10:23 AM 0
Share

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

C# rotating a 3D object left&right using mousedrag 0 Answers

EventSystem IDragHandler not working on iOS 1 Answer

How do i make a continuos touch that triggers more then one "button"? 1 Answer

Finding the centre of two touches 2 Answers

Can I Use This Code For IOS? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges