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 Lazrick · Sep 15, 2014 at 08:28 PM · c#cameratouchdrag

Drag Object, same speed as mouse/touch

I am trying to drag an object when left clicking on it or when touching it on a Android device. The problems I have is that the object is moving faster or slower than my mouse.

I found a little script that is almost working, but I still have a problem. When I drag my object, his position sometime jump suddently and my object keeping moving when I drag my mouse, but it is no more on my mouse cursor. While dragging, my object is jumping on and away from my cursor randomly.

Can anyone help me solve this or explain me why this happen.

Here is the code. I need to use perpective camera too.

     using UnityEngine;
     using System.Collections;
     
     public class Drag_Object : MonoBehaviour {
     
         RaycastHit hit; 
         
         // Use this for initialization
         void Start () {
             hit = new RaycastHit(); 
         }
         
         // Update is called once per frame
         void Update () {
             
         }
         
         void OnMouseDrag(){
             
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if (Physics.Raycast(ray, out hit, 1000.0f)) {
                 Vector3 newpos = new Vector3(hit.point.x, hit.point.y, gameObject.transform.position.z);
                 gameObject.transform.position = newpos;
             }
             
         }
     }
Comment
Add comment · Show 1
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 Mari shankari · Oct 22, 2015 at 08:19 AM 0
Share

Hello, I am using this code for my created GameObject. But the problem is that i'am not able to achieve this code. when i drag the mouse my gameobject is not moving.. what should be done??

2 Replies

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

Answer by Lazrick · Sep 16, 2014 at 06:12 PM

Thx robertbu. I did try your code and with some tune up, I could make it works perfectly for my need. I found another solution too that I wanted to share.

The "linkedObject" is an object that is moving in same time as the dragged object. I did that because for a special reason, I couldn't make the object child of the dragged object. You can remove it if you don't need.

Here my current solution :

 using UnityEngine;
 using System.Collections;
 
 public class DragObject : MonoBehaviour {
 
     private float dist;
     private Vector3 v3Offset;
     private Plane plane;
     private bool ObjectMouseDown = false;
     public GameObject linkedObject;
     
     void OnMouseDown() {
         plane.SetNormalAndPosition(Camera.main.transform.forward, transform.position);
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         float dist;
         plane.Raycast (ray, out dist);
         v3Offset = transform.position - ray.GetPoint (dist);     
         ObjectMouseDown = true;
     }
     
     void OnMouseDrag() {
         if (ObjectMouseDown == true){
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             float dist;
             plane.Raycast (ray, out dist);
             Vector3 v3Pos = ray.GetPoint (dist);
             v3Pos.z = gameObject.transform.position.z;
             v3Offset.z = 0;
             transform.position = v3Pos + v3Offset;
 
             if (linkedObject != null){ 
                 linkedObject.transform.position = v3Pos + v3Offset;
             }
         }
     }
 
     void OnMouseOut() {
         ObjectMouseDown = false;
     }
 
 }
 

Thx again for your help.

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
1

Answer by robertbu · Sep 16, 2014 at 01:57 AM

Your code will use the hit on the surface of the cube as the position to set the object. This means 1) you will end up with a jump the first time the object is clicked, and 2) the object will move closer since the hit is not on the same plane as the object. Actually you want the plane of movement to match the plane of the camera. For an axes aligned camera, it can be done with ScreenToWorldPoint(), or you could create a Quad and use Collider.Raycast(), but a more general solution is to use Unity's mathematical Plane class. The resulting drag code will work for arbitrary camera rotation. Example:

 using UnityEngine;
 using System.Collections;
 
 public class Drag_Object : MonoBehaviour {
 
     private Vector3 offset;
 
     void OnMouseDown() {
         offset = transform.position - GetHitPoint();
     }
     
     void OnMouseDrag(){
         transform.position = GetHitPoint() + offset;
     }
 
     Vector3 GetHitPoint() {
         Plane plane = new Plane(Camera.main.transform.forward, transform.position);
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         float dist;
         plane.Raycast (ray, out dist);
         return ray.GetPoint (dist);
     }
 }


Note that in general when using Plane.Raycast(), the return value needs to be checked to make sure the plane was hit, but given Camera.main.transform.forward as the normal, as long as the object is in view (which it will be if OnMouseDown/OnMouseDrag fires), then the plane cannot be missed.

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 xpkoalz · Jan 17, 2015 at 08:49 AM 0
Share

Thank you robertbu !! The code works, was having a hard time trying to figure out why Input.$$anonymous$$ousePosition is inaccurate on mobile devices...

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

Distribute terrain in zones 3 Answers

Touch - drag different objects 1 Answer

Dragging Camera based on Touch 0 Answers

Why does my app function correctly in Unity Remote but not compiled as an app? 0 Answers

Camera up and down on TouchField 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