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 macdude2 · May 24, 2011 at 12:45 AM · rigidbodyiphonetouchdrag

Detect screen tap vs. drag

I have tried to edit the drag iphone script to get it so that when I touch the object, the player picks it up, and when it touches again, the object drops. This works, but I do not want the cube to be picked up or dropped if the player drags his finger before or after touching the object. I just cannot make it work. The script i have seems to only reference one or the other, either touchphase.began or touchphase.ended, but it will not register both. How can I solve this issue? The script is below.

 using UnityEngine;
 using System.Collections;
 
 public class DragIphone : MonoBehaviour {
 
     public float spring = 50.0f;
     public float damper = 5.0f;
     public float drag = 10.0f;
     public float angularDrag = 5.0f;
     public float distance = 0.2f;
     public bool attachToCenterOfMass = false;
     public Transform BoxPosition;
     private bool touched = false;
     private SpringJoint springJoint;
     private Touch touch;
     private Touch touch1;
     private Vector2 tposition; 
     
     void Update (){
         // Make sure the user touched the screen
 
         if (Input.touchCount==0){
             return;
         }
         
         Camera mainCamera = FindCamera();
             
         // We need to actually hit an object
         RaycastHit hit = new RaycastHit();
     
         touch = Input.GetTouch(0);
       //  touch1 = Input.GetTouch(1);
         if(touch.phase == TouchPhase.Began){
             print("began");
                               tposition = touch.position;
             if (!Physics.Raycast(mainCamera.ScreenPointToRay(touch.position),  out hit, 100.0f))
                     return;
             
             // We need to hit a rigidbody that is not kinematic
             if (!hit.rigidbody || hit.rigidbody.isKinematic)
                 return;
             
             
             if (!springJoint){
                 GameObject go = new GameObject("Rigidbody dragger");
                 
                 //Rigidbody is automatically added by SpringJoint
                 
                 springJoint = (SpringJoint)go.AddComponent ("SpringJoint");
                 go.rigidbody.isKinematic = true;
             }
             
             springJoint.transform.position = hit.rigidbody.transform.position;
               
             if (attachToCenterOfMass){
                 Vector3 anchor =  hit.rigidbody.transform.position;
               //  transform.TransformDirection(hit.rigidbody.centerOfMass)
                 anchor = springJoint.transform.InverseTransformPoint(anchor);
                 springJoint.anchor = anchor;
             }else{
                 springJoint.anchor = Vector3.zero;
             }
             
             springJoint.spring = spring;
             springJoint.damper = damper;
             springJoint.maxDistance = distance;
             springJoint.connectedBody = hit.rigidbody;
             
             StartCoroutine("DragObject", hit.distance);
              
         }
     }
     
     private int count;
     
     void start(){
         count = 0;
     }
     
     
     IEnumerator DragObject (float distance){
            if (touch.phase == TouchPhase.Began && touched == true){
                print("yes");
                             touched = false;}     
 
         else if(touch.phase == TouchPhase.Began && touched == false){
             touched = true;
         }
         
       // else if(touch.phase == iPhoneTouchPhase.Began && touched == true){
            //        touched = false;
           //    }
         
           
             float oldDrag = springJoint.connectedBody.drag;
             float oldAngularDrag = springJoint.connectedBody.angularDrag;
             springJoint.connectedBody.drag = drag;
             springJoint.connectedBody.angularDrag = angularDrag;
             Camera mainCamera = FindCamera();
             
             while (touched == true){
               //     touch.phase != iPhoneTouchPhase.Ended &&
                 //   touch.phase != iPhoneTouchPhase.Canceled            
                     Ray ray = mainCamera.ScreenPointToRay(touch.position);
                 springJoint.transform.position = new Vector3(BoxPosition.transform.position.x, BoxPosition.transform.position.y, BoxPosition.transform.position.z);
                 yield return 0;
             
             }
             if (springJoint.connectedBody)
             {
                 springJoint.connectedBody.drag = 0;
                 springJoint.connectedBody.angularDrag = 0;
                 springJoint.connectedBody = null;
             }
         
     }
     
   
     
     Camera FindCamera (){
         if (camera)
             return camera;
         else
             return Camera.main;
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jahroy · May 24, 2011 at 12:52 AM

   var firstFinger = Input.GetTouch(0);
 
   if ( firstFinger.phase == TouchPhase.Moved ) {
 
     Debug.Log("I am dragging my finger...");
 
   }
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 macdude2 · May 24, 2011 at 02:18 PM 0
Share

I have done that, but then below in the ienumerator function all other phases other than that phase are not detected. Why does this happen?

avatar image
0

Answer by crevelop · Aug 18, 2011 at 10:44 PM

I've made few tutorials on dragging and rotating an object by touch(Unity iOS).

These are the Youtube videos:

Dragging http://youtu.be/QjUhQ4z6pF0

Rotating http://youtu.be/oOfPMKdJdKk

Get the source code from my website:

http://www.revelopment.co.uk

Hope it helps.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to stop Kinematic Rigidbody from moving through walls 2 Answers

How do i make a continuos touch that triggers more then one "button"? 1 Answer

Touch and drag an object 0 Answers

Click Drag not working on iphone 2 Answers

What's the matter with this script? Detect object dragging with iPhone 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