• 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 maryhary · Apr 11, 2012 at 02:12 PM · iphonetouchmove

Moving objects to point of touch on iPhone.

Hello. I'm hoping someone will point me in the right direction, even if this might be a dumb question... I'm working on an iPhone AR app and I'm able to get an object to show up where I tap my finger. But I would like it to move rather than just show up! So I guess I should use Translate, not position, but I can't get that to work with the ScreenToWorldPoint thing. Tried numerous different combinations of code.

Is this maybe much more complex than I think?

I'd be very grateful if somebody could help me out!

Iv'e read this post (among many others) but it doesn't get me all the way there... http://answers.unity3d.com/questions/225988/moving-an-object-to-the-position-of-touch.html

function Update () {

 for(var touch : Touch in Input.touches) {
 if(touch.phase == TouchPhase.Ended) {
     Debug.Log("Touched...");
     
     
     var curX = Input.GetTouch(0).position.x;
     var curY = Input.GetTouch(0).position.y;
     
     //transform.Translate = Camera.main.ScreenToWorldPoint(new Vector3 (curX, curY, 1000));
     transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (curX, curY, 1000));

 } 
 }
 }

EDIT: I've now been trying to combine Vector3.MoveTowards or Vector3.Lerp with Camera.main.ScreenToWorldPoint. Can this be done?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gregzo · Apr 11, 2012 at 02:26 PM

You're only checking when the user releases a finger (TouchPhase.Ended). To Translate effectively, start a coroutine (don't try to run this code!):

 if(touch.phase == TouchPhase.Ended) {
   
 MoveObjectHereInXSeconds(Input.GetTouch(i).position, 1.2); //1.2 s for example
 }
 }
 function MoveObjectHereInXSeconds(fingerPos:Vector2, time : float)
 {
     //write your own function here, starting with while(theObjectIsNoteWhereIWantIt)
     //MoveIt
     //Don't forget to yield;
 }
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
avatar image
0

Answer by JayMHelpsU · Apr 11, 2012 at 03:01 PM

Ok well is it more of a glide motion you are looking for? If so try this out.

var speeds : Int

for(var touch : Touch in Input.touches) { if(touch.phase == TouchPhase.Ended) { Debug.Log("Touched..."); var curX = Input.GetTouch(0).position.x; var curY = Input.GetTouch(0).position.y; Pos = Instantiate(curX, curY, 1000); Pos.rigidbody.AddForce(tran­sform.forward * speeds); } }

Try this

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 maryhary · Apr 12, 2012 at 06:57 AM 0
Share

Hi! I tried this, but I get the error message BCE0044: unexpected char: 0xAD. at this line: Pos.rigidbody.AddForce(tran­sform.forward * speed);

Can't make out what the problem is...

avatar image maryhary · Apr 12, 2012 at 07:24 AM 0
Share

Ok, so I changed to FixedUpdate, which got rid of that message. But now it has a problem with "Pos". Says it's an $$anonymous$$ identifier...

avatar image
0

Answer by Adamcbrz · Apr 11, 2012 at 02:29 PM

I am assuming you want it to move to the point you tap not just jump to the position. If thats the case you will have to set a variable to be your destination and then Translate over a set time. This is not tested code but can get you in the right direction. Another option is to use an code animation tool like iTween.

 var destination : Vector3;
 var speed : float = 10;
 function Update() {
     if(touch.phase == TouchPhaseEnded)
     {
         var curX = Input.GetTouch(0).position.x;
         var curY = Input.GetTouch(0).position.y;
         destination = Camera.main.ScreenToWorldPoint(new Vector3 (curX, curY, 1000));
     }

     if(destination != transform.position)
     {
         transform.position = (transform.position - destination).normilize * speed;
     }
 }
Comment
Add comment · Show 1 · 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 maryhary · Apr 12, 2012 at 08:33 AM 0
Share

Hello! I tried this (but normalized ins$$anonymous$$d of normilize) and it runs fine, but the object doesn't move at all. Doesn't even jump...

avatar image
0

Answer by maryhary · Apr 11, 2012 at 03:00 PM

I will look into this, but I just want to check what you mean by "You're only checking when the user releases a finger (TouchPhase.Ended)". What I want is to tap the screen, and then for an object to move to the location of the tap, is this not correct then? Just making sure you don't think I mean dragging things :)

Thank you for your time!

Comment
Add comment · Show 1 · 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 gregzo · Apr 11, 2012 at 03:34 PM 0
Share

@maryhary Please use comments and don't post a new answer in such cases...

And indeed, I don't mean dragging. In your script, if(touch.phase == TouchPhase.Ended) implies that all follwoing instructions (included in the curly braces, of course) will only be executed once, during the frame the finger was lifted in. Of course, to "glide" an object, you need to update it's position more than once!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Degree from point? 2 Answers

move a gameobject from a GUITexture 0 Answers

character touch/tap-to-move not interact with camera drag movement 0 Answers

Double tap on iPhone very unreliable 2 Answers

duel touch 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