- Home /
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 also
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");
}
}
}
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.
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.
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
@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
Can you specify what is not working? Also a screenshot of your collider component would be great.
@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
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.