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 Tyler Starr · Feb 07, 2011 at 06:42 AM · iphonetouchdrag-and-drop

Touch To Move iPhone

I would like a script to move a game object on the x and y axis. I tried to use the DragTransform script in the Standard Assets folder. But it doesn't move with the touch and seems laggy and on top of that it is really hard to touch... maybe iPhone mouse emulation is not very accurate.

Is there a script to do this (on iphone) or a tutorial? If not can I have some pointers on how to do this.

THANKS!!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Andre-Odendaal · Feb 07, 2011 at 09:05 AM

You can use Input.touches which returns deltaPosition for each touch and apply that to your transform.

// C# Code Snippet // Update is called once per frame void Update () { // Only if there are touches if (Input.touches.Length > 0) { // Only work with the first touch // and only if the touch moved since last update if (Input.touches[0].phase == TouchPhase.Moved) { float x = Input.touches[0].deltaPosition.x * speed * Time.deltaTime; float y = Input.touches[0].deltaPosition.y * speed * Time.deltaTime;

         transform.Translate(new Vector3(x, y, 0));
     }
 }

}

and here is a JavaScript code snippet:

// JavaScript Code Snippet function Update () { // Only if there are touches if (Input.touches.Length > 0) { // Only work with the first touch // and only if the touch moved since last update if (Input.touches[0].phase == TouchPhase.Moved) { var x = Input.touches[0].deltaPosition.x * speed * Time.deltaTime; var y = Input.touches[0].deltaPosition.y * speed * Time.deltaTime;

             transform.Translate(new Vector3(x, y, 0));
         }
     }

}

Please note this is just a snippet. If you want to directly copy this code please add a speed variable to your script before running.

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 Tyler Starr · Feb 07, 2011 at 05:09 PM 0
Share

It had about 5 errors. -BCE0044: expecting :, found 'x'. -Unexpected token: if. -Unexpected token: if. -UCE0001: ';' expected. Insert a semicolon at the end. -UCE0001: ';' expected. Insert a semicolon at the end.

avatar image Andre-Odendaal · Feb 08, 2011 at 05:24 AM 0
Share

Oops, I should have mentioned this is a C# code snippet. You also need to declare a speed float if you want to directly copy it.

avatar image RadhaUnity3d · May 07, 2013 at 12:29 PM 0
Share

can u please zip the file and send did to meee...

avatar image Akshay_ROG · Oct 05, 2013 at 03:16 AM 0
Share

A'ight,I have a question:I haven't tested this code but reading it a notion struck me, as we move our finger further the x value increases and thus the speed of transform movement, right? so what if we want this : if the touch is on left side of the screen and no matter how lengthy the distance of touch move be but the transform will lerp from 0 speed to a constant speed and stays until touch is canceled...

avatar image mcfea · Feb 25, 2014 at 05:11 AM 0
Share

straight and simple!

avatar image
0

Answer by Core · Jun 08, 2011 at 05:18 PM

Brilliant! Works like a charm! Thanks Andre!

Comment
Add comment · Show 2 · 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 ankush kushwaha · Nov 24, 2012 at 07:04 AM 0
Share

Thanks. really helpfull

avatar image LukaKotar · Nov 24, 2012 at 08:21 AM 0
Share

Please consider marking the answer with the tick icon if it helped you.

avatar image
0

Answer by przem997 · Aug 12, 2013 at 07:39 AM

Anywere touch move Object, this must be touch on object then can move... is not?

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

translating a touchDeltaPosition from the y axis to z axis 1 Answer

Massive Touch Lag on mobile devices? 3 Answers

Detecting Touch on Iphone and Android 1 Answer

Joystick C# change touch zone? 0 Answers

How to detect Game Object is touched in Unity iPhone? 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