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 /
This question was closed May 26, 2016 at 04:03 PM by cimo for the following reason:

Other

avatar image
0
Question by cimo · Dec 17, 2012 at 08:49 PM · mousetouchdragplanefix

Help to fix my drag code [SOLVED]

Hello all, i'm new to unity3d (fantastic engine and tools!!). I write this code for drag cube in a scene:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour
 {
     Plane plane;
     GameObject cube1;
     
     Vector3 mousePos;
     Touch finger;
     
     Ray ray;
     //RaycastHit hit;
     
     private Vector3 offset;
     
     // Public function
         // Use this for initialization
         void Start()
         {
             plane = new Plane(Vector3.up, Vector3.zero);
             cube1 = GameObject.Find("Cube1");
         }
         
         // Update is called once per frame
         void Update()
         {
             if (Input.GetButton("Fire1"))
             {
                 cube1.gameObject.transform.position = toMousePos();
             }
             
             /*if (Input.touches.Length == 1)
             {
                 toMousePos();
                 
                 if (finger.phase == TouchPhase.Moved)
                 {
                     //cube1.gameObject.transform.Translate(Time.deltaTime * 100.0f * new Vector3(finger.deltaPosition.x, 0, finger.deltaPosition.y));
                     cube1.gameObject.transform.position = Time.deltaTime * 100.0f * new Vector3(finger.deltaPosition.x, 0, finger.deltaPosition.y);
                 }
             }*/
         }
     // Public function
     
     // Private function
     private Vector3 toMousePos()
     {
         mousePos = Input.mousePosition;
         
         /*if (Input.touches.Length == 1)
         {
             finger = Input.touches[0];
         }*/
         
         ray = Camera.main.ScreenPointToRay(mousePos);
         //ray = Camera.main.ScreenPointToRay(finger.position);
         
         float distance;
         
         if (plane.Raycast(ray, out distance))
         {
             Debug.Log(distance);
             
             return ray.GetPoint(distance);
         }
         
         return Vector3.zero;
     }
     // Private function
 }

  • It works well, but has problem with the object "y", when i press a mouse button the drag start at object center and not bottom.... How to fix this? (The value of y should not change!).

  • Look the comment (i try to use touch). When i test this on my touch, the cube move at plane center and when i re-try to move, return always on plane center (mentions movements).

And... my code is right in this way (i'm on the right road) or exist "professional alternative" for drag?.

Thanks!!!

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

  • Sort: 
avatar image
0
Best Answer

Answer by Catlard · Dec 18, 2012 at 11:05 AM

If you want the object to not jump to the center, then you have to record the "offset" in a vector3 based on where the user clicks. The object should move to a position relative to where you click, not exactly to that point. That's your trouble, I think. I don't know nothin' about touches, and I don't understand your second question.

Also, clean up your code, please! That's probably your biggest problem -- that your code has all that commented-out code.

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 cimo · Apr 11, 2013 at 05:31 PM 0
Share

Thanks for the reply. I edit my code and i use this for position:

 Ray ray;
 Raycast raycastHit;

 private void toScreenPointToRay()
 {
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     Physics.Raycast(ray, out raycastHit);
 }
 
 cube1.transform.position = raycastHit.point;

Work very well!! Thanks.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Rotate on drag for IOS? 1 Answer

Ragdoll 2D character controller help 0 Answers

Click and Drag Camera 3 Answers

how to drag an object using 2d raycasts 0 Answers

Mobile touch dragging from mouse dragging 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