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 nathan · Dec 14, 2012 at 08:32 PM · raycast3ddrag

how to drag an object in 3d world with finger

Hi,

I am working on unity now and I want to drag a cube with my finger on the mobile device like tablet. Now I can drag the cube using raycast but the cube only move in 2d world, only the x, y coordination changes, I want to be able to drag the cube in 3d world, wish all the x, y, z axis can change. Thank you.

 if(Input.touchCount == 1)
         {
            Touch touch = Input.GetTouch(0);        
            ray = Camera.main.ScreenPointToRay(touch.position);    
            LayerMask layerMask = 1 << 9;
            if(Physics.Raycast(ray,out hit,100,layerMask)&&Input.GetTouch(0).phase == TouchPhase.Moved)
            {
               
                 //probe = hit.transform;  
                 hitObjects = hit.transform;
                 isHit = true;
                 hit.transform.Translate(Input.GetTouch(0).deltaPosition*Time.deltaTime);
                 //hit.transform.position = touch.deltaPosition*0.2f;
                   //hit.transform.position += Input.GetTouch(0).deltaPosition*0.02f;              
            }
         }
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 DaveA · Dec 14, 2012 at 08:34 PM

I would find the object's Viewport space coords (Camera.main.WorldToViewport()), move it in viewport coords, then convert that back to world coords (Camera.main.ViewportToScreen()). Keep the same Z value, just alter x,y, so the object moves in the plane that is z units from the camera, no matter which way the camera is looking.

Comment
Add comment · Show 7 · 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 nathan · Dec 14, 2012 at 09:08 PM 0
Share

That helped, thank you. I have one more question. I can drag the cube on touch screen with finger but it isn't smooth. $$anonymous$$g., if I move my finger too fast, the cube can't follow my finger. if I move it slowly, the cube move really fast. I want the dragging to be as smooth as possible. whenever i move my finger, the cube will stay with it, same speed, same position. it will be really appreciated if you can provide me some code.

if(Input.touchCount == 1) { Touch touch = Input.GetTouch(0); ray = Camera.main.ScreenPointToRay(touch.position); Layer$$anonymous$$ask layer$$anonymous$$ask = 1 << 9; if(Physics.Raycast(ray,out hit,100,layer$$anonymous$$ask)&&Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
//probe = hit.transform;
hitObjects = hit.transform; isHit = true; hit.transform.Translate(Input.GetTouch(0).deltaPosition*Time.deltaTime); //hit.transform.position = touch.deltaPosition*0.2f; //hit.transform.position += Input.GetTouch(0).deltaPosition*0.02f;
} }

avatar image DaveA · Dec 14, 2012 at 09:16 PM 0
Share

You might want to edit your original question and post/format the code snippet there. $$anonymous$$y guess is you are only hitting/moving when finger is over the object, and when finger is moving, it can get ahead of the object, stopping it. I would detect a 'select' on finger-down, and not check for hit on finger-drag, so you can move your finger anywhere and set the position to that spot.

avatar image DaveA · Dec 14, 2012 at 09:26 PM 0
Share

Right. Yeah I would have a 'selected' boolean which is set if the phase is 'begin' and ray hits it, and set false on phase is 'end', but don't do the raycast if phase is 'move'. And only move if selected is true.

avatar image nathan · Dec 14, 2012 at 09:26 PM 0
Share

i cast a ray from camera to the cube and drag the cube as finger touches the cube and move it. It should work under the move phase,right?

avatar image nathan · Dec 14, 2012 at 09:28 PM 0
Share

i don't want the result that wherever i touch the screen and the cube will be there. I really wanna the dragging effects.

Show more comments

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

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

Drag Object Via Axis Handles 0 Answers

A node in a childnode? 1 Answer

Curve a Ball in 3d space 4 Answers

Why don't have any dinamic shadows with -force-opengl? 0 Answers

Creating a World? 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