Log in or register to post comments

3D Model Drag

March 18, 2015 - 12:59am #1

I'm using below code and when i drag the 3d model using finger, the model is moving slow. So, how can i increase the speed of the movement properly.

private float maxPickingDistance = 2000;// increase if needed, depending on your scene size
 
private Transform pickedObject = null;
 
Ray ray = Camera.main.ScreenPointToRay(touch.position);
 
if (touch.phase == TouchPhase.Began) 
{
Debug.Log("Touch phase began at: " + touch.position);
 
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, maxPickingDistance)) 
pickedObject = hit.transform;                    
else
{
pickedObject = null;
}
else if (touch.phase == TouchPhase.Moved) 
{
Debug.Log("Touch phase Moved");
 
if (pickedObject != null) 
{
Vector2 screenDelta = touch.deltaPosition;
 
float halfScreenWidth = 0.5f * Screen.width;
float halfScreenHeight = 0.5f * Screen.height;
 
float dx = screenDelta.x / halfScreenWidth;
float dy = screenDelta.y / halfScreenHeight;
 
Vector3 objectToCamera = 
pickedObject.transform.position - Camera.main.transform.position;
float distance = objectToCamera.magnitude;
 
float fovRad = Camera.main.fieldOfView * Mathf.Deg2Rad;
float motionScale = distance * Mathf.Tan(fovRad/2);
 
Vector3 translationInCameraRef = 
new Vector3(motionScale * dx, motionScale * dy, 0);
 
Vector3 translationInWorldRef =
Camera.main.transform.TransformDirection(translationInCameraRef);
 
pickedObject.position += translationInWorldRef;
}

 

Log in or register to post comments