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 mark 1 · Jun 19, 2010 at 04:34 PM · dragiphonetouch

Using touch doesn't get the correct vector.

This is my code when I move my finger to move the object it does not follow my finger instead it goes right of the iphone screen too far. How do I make this object follow my finger?

var speed : float = 0.1; var detectingMotion : boolean; var arr : Array = new Array(); private var hit : RaycastHit; private var touchDeltaPosition:Vector2; private var touchedObject : Transform;

function Update () { for(touch in iPhoneInput.touches) { ///make one if statement if(iPhoneInput.touchCount == 1){ // detecting a one finger touch input if( touch.phase == iPhoneTouchPhase.Began){ //casting a ray from the touch location into the game

     ///create an ray for finger hit
     //var detectRay : Ray = Camera.main.ScreenPointToRay(touch.position);
     var detectRay : Ray = Camera.main.ScreenPointToRay(Vector3( touch.position.x, touch.position.y ));
     if (Physics.Raycast (detectRay, hit)) {
         print("You are touching the object "+ hit.transform.position);

         ///start the motion off
         detectingMotion = true;
         touchedObject = hit.transform;

     }
 }

         if( touch.phase == iPhoneTouchPhase.Moved && detectingMotion == true)
         { 
             // Get movement of the finger since last frame
             touchDeltaPosition = iPhoneInput.GetTouch(0).deltaPosition;

             // Move object across XY plane
             touchedObject.Translate (touchDeltaPosition.x, touchDeltaPosition.y, 0);

             ///record the movements
             print("You are moving the object y:" + touchDeltaPosition);
             //print("You are moving the object y:" + touchDeltaPosition.x + " y:"+ touchDeltaPosition.y);

             //add the poitions to the array.
             arr.Add (touchDeltaPosition);
         }

         if( touch.phase == iPhoneTouchPhase.Ended)
         {
             ///stop recording
             //print("You let go" + touchedObject.position);
             detectingMotion = false;
             ///print out movement array

              for (var pos : Vector2 in arr)
               { 
              print("moving this object after finger off"+pos.x+ ":x "+pos.y+":y");
             // Move object across XY plane
             //touchedObject.Translate (pos.x * speed, pos.y * speed, 0);
             }

         }
     }
  }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tetrad · Jun 19, 2010 at 05:05 PM

You have to make sure your coordinate systems are correct and consistent.

In particular, these two lines will need to be adjusted.

// Get movement of the finger since last frame touchDeltaPosition = iPhoneInput.GetTouch(0).deltaPosition;

// Move object across XY plane touchedObject.Translate (touchDeltaPosition.x, touchDeltaPosition.y, 0);

deltaPosition is in screen space. Translate moves the object in local space. I don't know how your camera and scene are set up, but if you move your finger right 20 pixels on the screen, you're telling your game object to move positive on its X axis by 20 world units.

That's most likely wrong in all cases, unless you have some kind of orthographic camera that 1 pixels = 1 unit. And always wrong if your camera is rotated. Basically the concept itself is wrong.

If you want to move the object to where your finger is pointing to in the game world, you'll probably want to do something like casting a ray using Camera.main.ScreenPointToRay, and placing your dragging object at the location returned from Physics.Raycast. If you do that you have to make sure you're not detecting a collision with the object you're dragging (otherwise it will move up every frame if it has any height to it at all). You can do that with layer masks, or use Physics.RaycastAll and just skip the collision result with the object you're dragging.

Comment
Add comment · Show 3 · 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 mark 1 · Jun 20, 2010 at 06:33 PM 0
Share

thanks for your suggestion have you got a example of this ?

avatar image Tetrad · Jun 20, 2010 at 07:15 PM 0
Share

Just do a search for "drag gameobject", there are a lot of questions on the same subject.

avatar image TedB · Jul 12, 2010 at 01:04 AM 0
Share

If there are, they are not easy to find. A search for "drag gameobject" on this site shows one question about dragging rigidbodies on Unity for iPhone, but the answer references a Wiki code snippet which is not for the iPhone and after several hours worth of try, I haven't gotten it to work in Unity iPhone 1.7. So it seems like a code example for this answer would be very helpful.

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

No one has followed this question yet.

Related Questions

OnDrag - Object lagging behind the mouse cursor. 0 Answers

Nested Scroll Views: How can I pass drag control from an inner scroll view to its outer scroll view? 2 Answers

How to use gameObject as a drag-able object. 1 Answer

UI drag and drop 1 Answer

drag object including free rotation 0 Answers


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