- Home /
Question by
richwebb8 · Jul 09, 2017 at 07:58 AM ·
androiddrag-and-dropphonesnapping
Problem snapping object on phone, but fine on PC.
I am developing for an android phone (Samsung S7) using google VR SDK for the cardboard.
I am trying to create a script that allows a cube to be dragged and dropped and then snapped to another cube when the objects are close enough. The script works perfectly when on the computer.
When I build and run on my phone, the cube can be dragged and dropped. However, the cube does not snap to the position of the second cube when close.
I have attached the whole script but I think it is the last if statement that is screwing up:
using UnityEngine;
using System.Collections;
public class DDCube : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
Ray ray;
float distance = Mathf.Infinity; //Infinite Distance
public void Update()
{
int layer_mask = LayerMask.GetMask("Cube");
ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f)); //Centre of the screen (i.e. reticle)
if (Physics.Raycast(ray, distance, layer_mask)) //Only hit if Cube
{
//At the frame the screen is touched
if (Input.GetMouseButtonDown(0))
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); //Where is the reticle in screen space?
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); //How far is the game object from reticle?
}
//Executed each frame whilst the screen is being touched
if (Input.GetMouseButton(0))
{
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 cursorPosition;
//Stop cube from moving outside boundary
cursorPosition.x = Mathf.Clamp(Camera.main.ScreenToWorldPoint(cursorPoint).x + offset.x, -4f, 4f);
cursorPosition.y = Mathf.Clamp(Camera.main.ScreenToWorldPoint(cursorPoint).y + offset.y, 0.5f, 4f);
cursorPosition.z = Mathf.Clamp(Camera.main.ScreenToWorldPoint(cursorPoint).z + offset.z, -4f, 4f);
gameObject.transform.position = cursorPosition; //Move cube to reticle position
GetComponent<Renderer>().material.color = Color.blue; //Change cube colour to blue
}
}
//At the frame the touch is released
if (Input.GetMouseButtonUp(0))
{
GetComponent<Renderer>().material.color = Color.red; //Change cube colour to red
GameObject Cube1 = GameObject.FindWithTag("Cube1");
GameObject Cube2 = GameObject.FindWithTag("Cube2");
var dist = Vector3.Distance(Cube2.transform.position, Cube1.transform.position);
var minDist = 2f;
//Snap Cube to Position
if (dist < minDist)
{
Cube1.transform.position = Cube2.transform.position;
Cube1.transform.rotation = Cube2.transform.rotation;
GetComponent<Renderer>().material.color = Color.green;
}
}
}
}
Thanks for the help, I am very new to unity so sorry if the script is structured horribly or anything.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612125829im_/https://answers.unity.com/themes/thub/images/avi.jpg)