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 josessito · May 02, 2014 at 10:33 PM · movementdragvectorpositioningradius

Radial distance between two points

Hi! I'm having a problem. I need to drag an object on the screen (x and y position). I'm dragging it with touch, but that's not the issue. The object needs to be dragged from the position of the touch, not it's center, also, the object's movement might be restricted by a radius and/or limited to move along an axis (x or y) and finally, if the finger leaves the circle defined by the radius, the object must keep following its relative position without getting out the radius.

To do this I'm using the code pasted below. It works like this: It creates an empty object in the position of the touch (dragMarker) and then it sets this dragMarker as the parent of the object to drag. Then, I update the postion of the dragMarker to track the touch. This works fine, but when I apply the radial restrictions things go wrong.

I believe that if I change the restriction radius (maxMovementRadius) to take into account the radial distance between the objects position (transform.position) and the dragMarker's position I can fix this, but I'm not sure how to do this (image). There may be a better approach to all this tough...

I'm using easy touch and there are some other functianlities in the code. You don't need to pay attention to the update function. Also, SetX and SetY are just custom extensions to transform that basically do what they say. (I have pasted those methods at the end of the code, but they would not work like that).

Thanks and sorry for the length of the question.

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Put this script on the object you want to be able to drag.
 /// It must be on the Touchable,the Floor or the Enemy layer.
 /// EasyTouch must be in the project.
 /// </summary>
 [AddComponentMenu("Utilities/Dragable Object")]
 public class DragableObject : MonoBehaviour
 {
 
     /// <summary>
     /// Allowed movement radius. If 0, the movement is free.
     /// </summary>
     public float maxMovementRadius;
     public bool lockXPosition;
     public bool lockYPosition;
     public float movementDamp = 10f;
     public bool useAndDestroy = false;
     public int maxPosibleUses = 1;
     
     
     [Range(0f,1f)]
     public float alpha = 1;
 
     private GameObject dragMarker;
     private Vector3 origPosition = Vector3.zero;
     private Vector3 origPositionMark = Vector3.zero;
     private bool used = false;
     
     private int usesCount = 0;
     private Color newColor = new Color();
     private ParticleSystem pufParticles;
     private LineRenderer lineRenderer;
 
     void OnEnable()
     {
         EasyTouch.On_Drag += On_Drag;
         EasyTouch.On_DragStart += On_DragStart;
         EasyTouch.On_DragEnd += On_DragEnd;
     }
 
     void OnDisable()
     {
         UnsubscribeEvent();
     }
 
     void OnDestroy()
     {
         UnsubscribeEvent();
     }
 
     void UnsubscribeEvent()
     {
         EasyTouch.On_Drag -= On_Drag;
         EasyTouch.On_DragStart -= On_DragStart;
         EasyTouch.On_DragEnd -= On_DragEnd;
     }
 
     void Awake()
     {      
           
         //save the original position of the object
         origPosition = transform.position;
         Debug.Log("Original position: " + origPosition);
         newColor = renderer.material.color;
         pufParticles = transform.Find("PufParticles").GetComponent<ParticleSystem>();
         
     }
 
     void Update()
     {
         
         newColor.a = alpha;
         renderer.material.color = newColor;
         
         if (useAndDestroy)
         {           
             if (PlayerController.ONOBJECT == gameObject)
             {
                 used = true;   
             }
             // if this conditions are meet it measn that the player has been over this object,
             //but it is not here anymore. So if it is used object, it will increment the uses count until 
             // it reachs tha max number of uses and it destroy itself            
             if (PlayerController.ONOBJECT!= gameObject && used)
             {
                 used = false;
                 usesCount++;
                 
                 if(usesCount >= maxPosibleUses)
                 {
                     SelfDestruct();
                 }
             } 
         }
     }
 
     // At the drag beginning 
     void On_DragStart(Gesture gesture)
     {
        
         // Verification that the action on the object
         if (gesture.pickObject == gameObject)
         {
             //play sound
             //play visual queue
             dragMarker = new GameObject("Drag Marker");
             dragMarker.transform.position = gesture.GetTouchToWordlPoint(transform.position.z, worldZ: true);
             dragMarker.transform.rotation = Quaternion.identity;
             
             gameObject.transform.parent = dragMarker.transform;
             origPositionMark = dragMarker.transform.position;
             Debug.Log("Drag Start. Marker Orig Position: " + origPositionMark);
         }
     }
 
     // During the drag
     void On_Drag(Gesture gesture)
     {
         if (gesture.pickObject == gameObject)
         {
 
             if (PlayerController.ONOBJECT != gameObject)
             {
                                
                 Vector3 position = gesture.GetTouchToWordlPoint(transform.position.z, worldZ: true);
                 Vector3 targetPos = Vector3.zero;
                  
                 targetPos.x = lockXPosition ? origPositionMark.x : position.x;
                 targetPos.y = lockYPosition ? origPositionMark.y : position.y;
               
                 if (maxMovementRadius != 0)
                 {
                     //this radious should be the magnited of the vector that starts in origPosition and ends in the marker??? 
                     if (Vector3.Distance(origPosition, position) < maxMovementRadius)
                     {
                         dragMarker.transform.SetX(Mathf.Lerp(dragMarker.transform.position.x, targetPos.x, Time.deltaTime * movementDamp));
                         dragMarker.transform.SetY(Mathf.Lerp(dragMarker.transform.position.y, targetPos.y, Time.deltaTime * movementDamp));
                         Debug.Log("In radious");
 
                     }
                     else
                     {
                         Debug.Log("out of radious");
 
                         Vector3 newPos = position - origPosition;
                         newPos.Normalize();                                                                                  
                         newPos *= maxMovementRadius;
                         newPos += origPosition;
 
                         newPos.x = lockXPosition ? origPositionMark.x : newPos.x;
                         newPos.y = lockYPosition ? origPositionMark.y : newPos.y;
 
                         dragMarker.transform.position = newPos;
 
                         ;
                     }
                 }
                 else
                 {
                     dragMarker.transform.SetX(Mathf.Lerp(dragMarker.transform.position.x, targetPos.x, Time.deltaTime * movementDamp));
                     dragMarker.transform.SetY(Mathf.Lerp(dragMarker.transform.position.y, targetPos.y, Time.deltaTime * movementDamp));
 
                 } 
             }
         }
     }
 
     // At the drag end
     void On_DragEnd(Gesture gesture)
     {
         //play sound
         //play visual queue
         transform.parent = null;
         Destroy(dragMarker);
     }
 
     void SelfDestruct()
     {
         pufParticles.Play();
         alpha = 0;
         collider2D.enabled = false;
     }
 }
 
 //extensiond
 
 public static class TransformExtensions
 {
     public static void SetX(this Transform t, float n)
     {
         t.position = new Vector3(n, t.position.y, t.position.z);
     }
 
     public static void SetY(this Transform t, float n)
     {
         t.position = new Vector3(t.position.x, n, t.position.z);
     }
 
     public static void SetZ(this Transform t, float n)
     {
         t.position = new Vector3(t.position.x, t.position.y, n);
     }
 
 }

alt text

problem.png (75.3 kB)
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
Best Answer

Answer by josessito · May 04, 2014 at 09:07 PM

Ok, this is embarrasing. I was doing things much more complicated than they needed to be. Here is the code. The approach is much simpler and logical. using UnityEngine; using System.Collections;

 /// <summary>
 /// Put this script on the object you want to be able to drag.
 /// It must be on the Touchable,the Floor or the Enemy layer.
 /// EasyTouch must be in the project.
 /// </summary>
 [AddComponentMenu("Utilities/Dragable Object")]
 
 public class DragableObject : MonoBehaviour
 {
 
     /// <summary>
     /// Allowed movement radius. If 0, the movement is free.
     /// </summary>
     public float maxMovementRadius;
     public bool lockXPosition;
     public bool lockYPosition;
     public float movementDamp = 10f;
 
     private Vector3 deltaPosition; 
     private Vector3 origPosition = Vector3.zero;
     
     
 
     void OnEnable()
     {
         EasyTouch.On_Drag += On_Drag;
         EasyTouch.On_DragStart += On_DragStart;
         EasyTouch.On_DragEnd += On_DragEnd;
     }
 
     void OnDisable()
     {
         UnsubscribeEvent();
     }
 
     void OnDestroy()
     {
         UnsubscribeEvent();
     }
 
     void UnsubscribeEvent()
     {
         EasyTouch.On_Drag -= On_Drag;
         EasyTouch.On_DragStart -= On_DragStart;
         EasyTouch.On_DragEnd -= On_DragEnd;
     }
 
     void Awake()
     {     
         //save the original position of the object
         origPosition = transform.position;
         Debug.Log("Original position: " + origPosition);
              
     }
 
     void Update()
     {
                           
     }
 
     // At the drag beginning 
     void On_DragStart(Gesture gesture)
     {   
         // Verification that the action on the object
         if (gesture.pickObject == gameObject)
         {
                                   
             Vector3 position = gesture.GetTouchToWordlPoint(transform.position.z, worldZ: true);           
             //here it is stored the delta betwwen thr touch and the objecs center position
             deltaPosition = position - transform.position;
 
         }
     }
 
     // During the drag
     void On_Drag(Gesture gesture)
     {
         if (gesture.pickObject == gameObject)
         {
 
             if (PlayerController.ONOBJECT != gameObject)
             {
                                
                 Vector3 position = gesture.GetTouchToWordlPoint(transform.position.z, worldZ: true);
                 Vector3 targetPos = Vector3.zero;
                  
                 targetPos.x = lockXPosition ? origPosition.x : position.x - deltaPosition.x;
                 targetPos.y = lockYPosition ? origPosition.y : position.y - deltaPosition.y;
               
                 if (maxMovementRadius != 0)
                 {
                      
                     if (Vector3.Distance(origPosition, (position-deltaPosition)) < maxMovementRadius)
                     {
                        
                         transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * movementDamp);
 
                     }
                     else
                     {
                         
                         Vector3 newPos = position - deltaPosition - origPosition;
                         newPos.Normalize();                                                                                  
                         newPos *= maxMovementRadius;
                         newPos += origPosition;
 
                         newPos.x = lockXPosition ? origPosition.x : newPos.x;
                         newPos.y = lockYPosition ? origPosition.y : newPos.y;        
                         
                         transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * movementDamp);
                     }
                 }
                 else
                 {                                         
                     transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * movementDamp);
                 } 
             }
         }
     }
 
     // At the drag end
     void On_DragEnd(Gesture gesture)
     {
         //play sound
         //play visual queue
    
     }
 
    
 }
 
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

20 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change game object position smoothly not snapping to the position 1 Answer

How to move object using Vector3.Cross? 0 Answers

Row Boat script 0 Answers

How to slow down a gameobject that has rigidbody 1 Answer

Clamping object movement in circle 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