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 /
This question was closed Sep 28, 2013 at 09:59 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by lblaz · Sep 28, 2013 at 05:28 AM · iosraycastinputtouchdrag

Help With Touch to Drag Script

I currently have a script to move an object by dragging it with the IOS touch input. I want the script to be able to move my object even if your finger is not touching the object. So you can be able to drag the object around even if your finger is below, on top of it... etc.. However, when I use this script and I touch like below or above and try to drag, it transforms the position of the object to my finger. Ill post the script and if someone can help me with it thatd be appreciated. Thanks

 #pragma strict
 
 function Start () {
 }
 function Update () {
     for (var touch : Touch in Input.touches){
         var ray = Camera.main.ScreenPointToRay(touch.position);
         var hit : RaycastHit;
 
         if (Physics.Raycast (ray, hit, 100)) {
 
             if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
 
                 var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0);
 
                 transform.position = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z - 0.5));
             }
         }
     }
 }
Comment
Add comment · Show 2
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 robertbu · Sep 28, 2013 at 05:33 AM 0
Share

Typically in TouchPhase.Began, you calculate an offset from the finger position to the object. As the finger is moved, you position at the finger position plus the offset.

And what's up with line 14? This calculation is just the position of the camera (Camera.main.transform.position).

avatar image lblaz · Sep 28, 2013 at 05:56 AM 0
Share

line 14 is used to make a var which is used in 16 to keep the dot under the camera.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by lblaz · Sep 28, 2013 at 06:24 AM

Solved. Changed the script var object : GameObject;

 var speed:float = 0.0001;
 
 function Update () {
 
     if (iPhoneInput.touchCount > 0 && iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {
  var touchDeltaPosition:Vector3 = iPhoneInput.GetTouch(0).deltaPosition;
    transform.Translate (touchDeltaPosition.x * speed/3, touchDeltaPosition.y * speed/3, 0);
 
     }
 
 }
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 robertbu · Sep 28, 2013 at 06:29 AM 0
Share

Solving it this way has two potential problems. First deltaPosition is in screen coordinates, so the amount it will move will vary depending on the platform. And second (related) problem is that since you are no longer using world coordinates, your finger will not stay in the same position relative to the object your are dragging.

avatar image AkooleGame · Jan 31, 2015 at 01:02 PM 0
Share

@robertbu - Can you please guide on how to fix this. I am having same issue.

Follow this Question

Answers Answers and Comments

15 People are following this question.

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

Related Questions

Touch drag objects, raycast cant detect object? 1 Answer

IOS object touch, collider 1 Answer

Tap to Move Drag to Look Null Reference 1 Answer

Move an object to Input.Touch location 0 Answers

Question about user input event handling in Unity game for iOS 2 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