Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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.

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

Simultaneous Touch Drag Controls 0 Answers

how do I create a 3d model viewer app using unity 0 Answers

Tap to Move Drag to Look Null Reference 1 Answer

Remove Y-axis clamp from Tap to Move, Drag to Look Script 0 Answers

Rotating a gameobject via dragging finger 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