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 haroonalam · Oct 06, 2011 at 01:24 PM · c#dragunityiphone

Object drags wherever I touch on screen- Iphone-Unity

Hey ppl. I am trying to drag my game object using iphone touch. I am having the problem that my object is dragging wherever on the screen I touch, I want it to drag only when placing my finger on object. Here is my code:

void Update () {

     foreach (Touch touch in Input.touches) {
      Ray ray = Camera.main.ScreenPointToRay (touch.position);
      

  if(touch.phase == TouchPhase.Began && Physics.Raycast(ray, out hit)) {
             
 if(hit.collider.gameObject.tag == "Player")            
 {            
     //translate the cubes position from the world to Screen Point
    screenSpace = Camera.main.WorldToScreenPoint(transform.localPosition);
            
        //calculate any difference between the cubes world position and the mouses Screen position converted to a world point
        offset = transform.localPosition - Camera.main.ScreenToWorldPoint(
        new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
             
          Debug.Log("Finger has been touched...");
    }
    }  
         
   if(touch.phase == TouchPhase.Moved && Physics.Raycast(ray, out hit,1000)) {

 if(hit.collider.gameObject.tag == "Player")
 {            
 
     //keep track of the mouse position
     Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
     
      //convert the screen mouse position to world point and adjust with offset
             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     
     //update the position of the object in the world
     transform.position = curPosition;
 } 
 }                
 }//for
 

Please help me with this

Comment
Add comment · Show 3
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 haroonalam · Oct 06, 2011 at 02:48 PM 0
Share

ok, I have done with it, just used hit.collider.gameObject.tag == "Player", but object is not dragging v smoothly on simulator, is this is bcz of simulator ?

avatar image noradninja · Oct 06, 2011 at 05:58 PM 0
Share

Are you updating the position on the moved phase or the stationary phase? If you want it to update constantly, use moved to get smooth position changes.

avatar image haroonalam · Oct 07, 2011 at 07:40 AM 0
Share

@noradninja, I have updated my code and now using moved, it is far smooth then previous one, but still not very smooth, if I remove 'hit.collider.gameObject.tag == "Player"' in TouchPhase.moved, then It moves v smoothly but on finger pressed anywhere on sreen, I want object to move when finger pressed on object. so what do you think is'nt it because of simlator or anything wrong in my code ?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by noradninja · Oct 07, 2011 at 12:42 PM

When I want to move a specific object, I create a variable to hold that object and assign it in the Editor, and then directly update the transform.position of that object to the location of my touch when phase=moved in the Update() function, that way I am directly calling the object to be moved; I am fairly certain using the gameObject.tag method has some overhead as Unity will have to 'find' your object, then test for a collider hit:

 var joyButton : GUITexture; //the actual object you are dragging
 var joyOrient : GameObject; //this should be the same as joyButton, used for location purposes only
 private var orientPosition : Vector3;
 private var storedID : int;

 function Awake () {
     
     orientPosition = joyOrient.transform.position; //set orient to initial location of joyOrient object
     
 } //End Awake
 
 function Update () {
 
 for (var event : Touch in Input.touches) { //check for touches
          
 var JoyHitTest = (joyButton.HitTest(event.position)); 
 
     if (event.phase == TouchPhase.Began || event.phase == TouchPhase.Moved) { //if phase is Began or Moved
             joyTouchID = event.fingerId; //get ID of latched touch
         if(JoyHitTest){ //and if you touched the joystick thumb
             latched = true; //touch is latched to stick
                     storedID = joyTouchID; //store ID of finger used to initiate this touch
                         
         } //End if
     
     } //End if
 
     if (event.phase == TouchPhase.Ended && event.fingerId == storedID){
         
         latched = false;
                
     } //End If
     
     if (latched==true){
     
         joyButton.transform.position.x = (event.position.x/1024); //set position of thumb to finger location on iPad screen
         joyButton.transform.position.y = (event.position.y/768); //must change this so resolution is variable driven 
         joyButton.transform.position.z = 0; //z axis is always zero since we only care about 2D movement here
     } //End If
 
     if (latched==false){
 
         joyButton.transform.position = orientPosition; //return joystick thumb to the orient location
             
     }//End If
 
 } //End for
 
 } //End Update

This will allow you to drag a GUITexture (joyButton) around the screen. Because touch location and object position use two different coordinate systems, we have to manually convert from one to the other, hence the division. You will want to adjust the 1024 and 768 to the resolution of your device; I generally make those numbers a variable like divisorX and divisorY so I can detect the device the end user is operating and adjust the numbers accordingly. If you implement this and you still have lag in the simulator, then it is because of the simulator; this code is tested and runs at 30FPS on a first gen iPhone. You should be able to make this work with a GO by changing the typing of joyButton to GameObject. Let me know if this helps.

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 saddam751 · Dec 10, 2013 at 07:01 AM 0
Share

i have implemented this code ....

pragma strict

var joyButton : GUITexture; //the actual object you are dragging var joyOrient : GameObject; //this should be the same as joyButton, used for location purposes only private var orientPosition : Vector3; private var storedID : int;

function Awake () {

 orientPosition = joyOrient.transform.position; //set orient to initial location of joyOrient object
 

} //End Awake

function Update () {

for (var event : Touch in Input.touches) { //check for touches

var JoyHitTest = (joyButton.HitTest(event.position));

 if (event.phase == TouchPhase.Began || event.phase == TouchPhase.$$anonymous$$oved)
  { //if phase is Began or $$anonymous$$oved
       var joyTouchID = event.fingerId; //get ID of latched touch
     if(JoyHitTest)
     { //and if you touched the joystick thumb
     var latched = true; //touch is latched to stick
     storedID = joyTouchID; //store ID of finger used to initiate this touch
 
    } //End if
 
 } //End if
 
 if (event.phase == TouchPhase.Ended && event.fingerId == storedID){
 
    latched = false;
 
 } //End If
 
 if (latched==true){
 
    joyButton.transform.position.x = (event.position.x/1024); //set position of thumb to finger location on iPad screen
    joyButton.transform.position.y = (event.position.y/768); //must change this so resolution is variable driven 
    joyButton.transform.position.z = 0; //z axis is always zero since we only care about 2D movement here
 } //End If
 
 if (latched==false){
 
    joyButton.transform.position = orientPosition; //return joystick thumb to the orient location
 
 }//End If
 

} //End for

} //End Update

i have added "var" in front of latched and joyTouchID because it was showing error. this code is not giving exact drag.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

C# DragRigidbody ArgumentException: get_main can only be called from the main thread 2 Answers

changing UITexture of A GameObject 1 Answer

Does Input.mousePosition works for 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