Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hawken-2 · Jul 12, 2011 at 10:44 AM · iosraycastcollidertouchdrag-and-drop

Dragging an object in iOS (UnityScript)

Does anyone have a solution for dragging colliders in iOS? I tried the following, which I think is not far off, compiles but doesn't work on the device.

 var titleCamera : Camera;
 var hit : RaycastHit;
 var ray : Ray;
 
 function Update () {
     if(Input.touchCount == 1) {
         ray = titleCamera.ScreenPointToRay(Input.touches[0].position);
         var touch : Touch = Input.touches[0];
         if (touch.phase == TouchPhase.Moved && Physics.Raycast(ray.origin, ray.direction,hit)) {
     
             var touchPos : Vector2 = Input.GetTouch(0).deltaPosition; // touch position
             var newPos : Vector2; //object position
             
             newPos = touchPos;
             
             switch(hit.collider.name){
                 
                 case "object01":
                     Debug.Log("object01 drag");
                      transform.Translate(newPos);
                 break;
                 case "object02":
                     Debug.Log("object01 drag");
                      transform.Translate(newPos);
                 break;
             }
         }
     }
 }

Any help much appreciated :) It's for detecting a drag on any object using a collider (I've not got around to using tags yet)

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
0

Answer by hawken-2 · Jul 13, 2011 at 07:35 AM

trying a few variations on this, but really don't know what to add for newPos. I have a mousedown version of this running just fine, looks like I'm going to have to give up on iPhone touch drag in Javascript for now... here's the code, that functions for mouse, but not for touch;

Any clues as to what to put in the newPos var would be great appreciated!

 var titleCamera : Camera;
 
 function OnMouseDown () {
     var screenSpace = titleCamera.WorldToScreenPoint(transform.position);
     var offset = transform.position - titleCamera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
     while (Input.GetMouseButton(0))
     {
         var clickPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
         var newPos = titleCamera.ScreenToWorldPoint(clickPos) + offset;
         transform.position = newPos;
         yield;
     }
 }
 
 var hit : RaycastHit;
 var ray : Ray;
 
 function Update () {
     if(Input.touchCount == 1) {
         ray = titleCamera.ScreenPointToRay(Input.touches[0].position);
         var touch : Touch = Input.touches[0];
         while (touch.phase == TouchPhase.Moved && Physics.Raycast(ray.origin, ray.direction,hit)){
             var touchPos : Vector2 = Input.GetTouch(0).deltaPosition;
             var newPos = touchPos;
             transform.position = newPos;
         }
     }
 }
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 efge · Nov 03, 2012 at 12:40 PM

Maybe I am a little bit late ...

That's what the docs say: "OnMouseDown, OnMouseEnter, OnMouseOver, OnMouseExit, OnMouseDown, OnMouseUp, OnMouseDrag events are not supported."

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 Anxo · Nov 03, 2012 at 01:09 PM

It might also be worth considering buying something like "Touches" from the asset store. While you CAN write it all yourself, there really is no need unless you need to do something very different. When I got my input touches package it only cost me 10 bucks and saved me a lot of days of trying to work out controls and gave me more time to focus on game design. There are is also the more popular "Finger Gestures" package but it is also more expansive.

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 Korpers · Aug 31, 2013 at 07:52 AM 0
Share

I just bought Touches based off of this, but can't get it to do anything that I want! :(

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to push object along Z axis with Ray on touch? 1 Answer

How to hit two object with one raycast? 2 Answers

Help with touch input for iOS please! 1 Answer

Raycast Hit not working 0 Answers

touch game objects on ios 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