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 karthees · Jul 23, 2014 at 06:56 AM · movementrotatevuforiadrag

How to drag the 3D model in screen with finger

I'm doing vuforia + unity apps. I have displayed the 3D model in the screen and referred the script from here https://developer.vuforia.com/resources/dev-guide/unity-drag-ar-object-screen-your-finger for drag the 3D model in the screen but nothing happen when i touch the screen and drag. Please check screenshot alsoalt text

alt text

alt text

 using UnityEngine;
 using System;
 
 public class DynamicModelsGUI : MonoBehaviour
 {    
     private GameObject metaioMan;
     public GUIStyle buttonTextStyle;
     float SizeFactor;
     
     // variables for ray tracing
     private RaycastHit hit;
     private LayerMask layerMask = 1<<4;
 
     private bool selected = false;
     private Vector3 offset = new Vector3(0.0f, 0.0f, 0.0f);
     
     void Start() 
     {
         SizeFactor = GUIUtilities.SizeFactor;
         
         // Find metaioman object
         metaioMan = GameObject.Find("HMI_Mirra_3D");
     }
     
     // Update is called once per frame
     void Update()
     {    
         SizeFactor = GUIUtilities.SizeFactor;
     
         // only handle touchs or clicks when metaioman is active, i.e.
         // is visible on tracking pattern
         if (metaioMan.active)
         {
             if (Input.touchCount > 0)
                 // if tracking, then evaluate touch point
                 handleTouches();
             else if (Input.GetMouseButton(0))
                 handleClicks();
             else
                 selected = false;
         }
     }
     
     private void handleTouches()
     {
         // if there's touch points and the phase is began-->try to select the geometry
         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
             selectGameObject(Input.GetTouch(0).position);
         
         // if the touch point is moving and metaio man is selected
         if(Input.GetTouch(0).phase == TouchPhase.Moved && selected)
             moveGameObject(Input.GetTouch(0).position);
         else if(Input.GetTouch(0).phase == TouchPhase.Ended)
             // deselect metaio man if touch has ended
             selected = false;
     }
     
     public void handleClicks()
     {
         // if there's left mouse click-->try to select the geometry
         if (!selected && Input.GetMouseButtonDown(0))
             selectGameObject(Input.mousePosition);
         
         if (selected && Input.GetMouseButton(0))
             moveGameObject(Input.mousePosition);
     }
     
     public void selectGameObject(Vector2 position)
     {
         // get a ray from the touch point
         Ray ray = Camera.main.ScreenPointToRay(position);
             
         // layer mask, metaio man is on layer 4
         layerMask = 1<<4;
             
         // cast a ray on layer 4, if metaio man has been hit
         if (Physics.Raycast(ray, out hit, 5000, layerMask) && hit.collider.gameObject.name == "HMI_Mirra_3D")
         {
             // metaio man is touched --> select it
             selected = true;
                 
             // swith to layer 8, find the initial touch point on the plane and use it as a reference
             layerMask = 1<<8;
             if (Physics.Raycast(ray, out hit, 5000, layerMask))
             {
                 // record the offset of the touch point and the object position point
                 offset = hit.point - metaioMan.transform.position;
             }
         }
         else
             // not hit, don't select
             selected = false;
     }
     
     public void moveGameObject(Vector2 position)
     {
         // cast a ray on layer 8 (the plane) to calculate the hit position of the plane
         Ray ray = Camera.main.ScreenPointToRay(position);
         layerMask = 1<<8;
         if(Physics.Raycast(ray, out hit, 5000, layerMask) && hit.collider.gameObject.name == "HMI_Mirra_3D")
         {
             // move the metaio man to the intersect of the ray and the plane, offset should be accounted for
             metaioMan.transform.position = hit.point - offset;
         }
     }
     
     public void OnGUI()
     {
         if(GUIUtilities.ButtonWithText(new Rect(
             Screen.width - 200*SizeFactor,
             0,
             200*SizeFactor,
             100*SizeFactor),"Back",null,buttonTextStyle) ||    Input.GetKeyDown(KeyCode.Escape)) {
             PlayerPrefs.SetInt("backFromARScene", 1);
             Application.LoadLevel("MainMenu");
         }        
     }
 }
3d model.png (24.3 kB)
screen shot 2014-07-31 at 3.34.29 pm.png (9.5 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

Answer by HarshadK · Jul 23, 2014 at 07:56 AM

You need to assign a mesh to your mesh collider component. Currently there is no mesh assigned to your mesh collider.

Check this out.

alt text

In the image below there is a cube mesh assigned for the mesh collider.

Since there is no mesh assigned to your mesh collider the RayCast is not working from your script which I suppose is the cause of issue.

Comment
Add comment · Show 12 · 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 karthees · Jul 23, 2014 at 09:11 AM 0
Share

I need to add my own 3D model into mesh?? how can i add? when i choose mesh my 3D model .obj is not appear

avatar image karthees · Jul 31, 2014 at 05:20 PM 0
Share

@Harshadk: I can't add .fbx or .3ds 3d model to mesh. Please help this..It's working with cube but it's not working with .fbx or .3ds

avatar image HarshadK · Aug 01, 2014 at 05:16 AM 0
Share

Can you specify what is not working? Also a screenshot of your collider component would be great.

avatar image karthees · Aug 06, 2014 at 04:49 AM 0
Share

@Harshadk: I can't apply mesh to H$$anonymous$$I_$$anonymous$$irra_3D. I can apply mesh collider to only sub parts like HArmPads, HArms etc. So, the fulle 3D model is not moving

avatar image HarshadK · Aug 06, 2014 at 05:51 AM 0
Share

You can apply mesh collider to your child objects (e.g. HArmPads), then when these parts are hit after a raycast, you can move their parent game object (H$$anonymous$$I$$anonymous$$irra3D) accordingly.

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

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

Related Questions

How do I rotate a game object and have it move in relation to the rotation angle? 2 Answers

Two Keys ws or wd detection 3 Answers

Issue with Camera Tilt & Movement at the same time 3 Answers

OnMouseUp only partially working in Android? 0 Answers

Mouse Controller 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