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 _milla_ · Jul 09, 2014 at 09:18 PM · touchdragoffsetsnapping

Touched object snaps to center when dragged

I already found a very good script in the forum concerning dragging a 3D object on an xz-Plane!

http://answers.unity3d.com/questions/566327/drag-object-relative-to-camera.html

 private float maxPickingDistance = 10000;// increase if needed, depending on your scene size
     
     private Vector3 startPos;
     private Vector3 offset;
     
     private Transform pickedObject = null;
     
     // Update is called once per frame
     void Update ()
     {
         foreach (Touch touch in Input.touches)
         {
             //Create horizontal plane
             Plane horPlane = new Plane(Vector3.up, Vector3.zero);
             
             //Gets the ray at position where the screen is touched
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
             
             if (touch.phase == TouchPhase.Began)
             {
                 RaycastHit hit = new RaycastHit();
                 if (Physics.Raycast(ray, out hit, maxPickingDistance))
                 {
                     pickedObject = hit.transform;
                     startPos = touch.position;
                 }
                 else
                 {
                     pickedObject = null;
                 }
             }
             else if (touch.phase == TouchPhase.Moved)
             {
                 if (pickedObject != null)
                 {
                     float distance1 = 0f;
                     //offset = pickedObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(startPos.x, startPos.y,pickedObject.transform.position.z));
                     if (horPlane.Raycast(ray, out distance1))
                     {
                         pickedObject.transform.position = ray.GetPoint(distance1);
                     }
                 }
             }
             else if (touch.phase == TouchPhase.Ended)
             {
                 pickedObject = null;
             }
         }
     }
 }

I just can't figure out how to fix the snapping to the center of the object when i start dragging. I found several Q/A in this forum where an offset is mentioned but when i try to implement any of those ideas in the code the debugger throws exceptions. I would appreciate any ideas!

Thanks in advance!

milla

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

Answer by _milla_ · Jul 10, 2014 at 07:58 PM

Alright - i tried again figuring out, how i can implement the offset. I almost got it. There is just one thing i could not get fixed. The setup allows dragging on the xz-plane. When I touch an object near the plane (y almost 0) and start dragging, it seams to work (object does not jump to my finger and is dragged very smooth). BUT when i touch the object at the top (e.g. at the tip of a tower) the more i drag it, the more it moves away from my touch point. It moves in the same direction, but the distance between object and touch becomes bigger the longer i drag it.

Can anybody help me or give some advices? Thanks in advance!!!

milla

Here is the code:

 using UnityEngine;
 using System.Collections;
 //code from http://answers.unity3d.com/questions/566327/drag-object-relative-to-camera.html
 
 public class MyDragBehaviour2 : MonoBehaviour
 {
     private float maxPickingDistance = 10000;// increase if needed, depending on your scene size
     
     private Vector3 offset;
     private float distance1;
     private Transform pickedObject = null;
     
     // Update is called once per frame
     void Update ()
     {
         foreach (Touch touch in Input.touches)
         {
             //Create horizontal plane
             Plane horPlane = new Plane(Vector3.up, Vector3.zero);
             
             //Gets the ray at position where the screen is touched
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
 
             if (touch.phase == TouchPhase.Began)
             {
                 RaycastHit hit = new RaycastHit();
                 if (Physics.Raycast(ray, out hit, maxPickingDistance))
                 {
                     horPlane.Raycast(ray, out distance1);
                     pickedObject = hit.transform;
                     offset = pickedObject.transform.position - ray.GetPoint(distance1); 
                 }
                 else
                 {
                     pickedObject = null;
                 }
             }
             else if (touch.phase == TouchPhase.Moved)
             {
                 if (pickedObject != null)
                 {
                     float distance1 = 0f;
 
                     if (horPlane.Raycast(ray, out distance1))
                     {
                         pickedObject.transform.position = ray.GetPoint(distance1)+offset;
                         
                     }
                 }
             }
             else if (touch.phase == TouchPhase.Ended)
             {
                 pickedObject = null;
             }
         }
     }
 }

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

2 People are following this question.

avatar image avatar image

Related Questions

How to apply offset to touch position when dragging object 4 Answers

Ragdoll 2D character controller help 0 Answers

Touch and Drag for Android problem 0 Answers

How to change the scaling of a gameobject during runtime? 2 Answers

I'm not sure how to approach this. 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