void Update()
	{

S_x_temp = slot.transform.position.x;

		S_y_temp = slot.transform.position.y;
		
		
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
		{
            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
	
			
		
			if(Vector3.Distance(touchDeltaPosition,slot.transform.position) <= 1)
			{
				slot.transform.position = new Vector3(touchDeltaPosition.x,touchDeltaPosition.y,10);
				cube.transform.position = new Vector3(S_x_temp,S_y_temp,10);

any one could tell me whats going wrong with this touch for android…

all i want to do is when one gameubject is touched then it should change it’s position with second gameobject…

only if the distance between them is 1 or less…

Any kind of help will be much appriciated…Thanks in Advance…if you are posting code then please go for c# if at all possible…Thanks Again You All Rock Cheers And Happy New Year

You said you want them to swap if the distance is less than or equal to 1.
But you wrote:

if((Vector3.Distance(touchDeltaPosition,slot.transform.position) == 1)

which only works when the distance is exactly equal to 1.
Replace the == operator with <= operator:

if((Vector3.Distance(touchDeltaPosition,slot.transform.position) <= 1)

Since I can see you are trying, here is a simple working example:

Step 1: Create a NEW, EMPTY project with a NEW, EMPTY SCENE

Step 2: In your project, create a (C#) script called “TileSwap”… (SEE SCRIPT BELOW)

Step 3: Attach the script to the main camera in your scene. (Don’t move the camera) Build and run.

:slight_smile:

Here is the script:

using UnityEngine;
using System.Collections;

public class TileSwap : MonoBehaviour {
	
	int gameSize = 5; 
	public Vector2 slot = new Vector2();
	// Use this for initialization
	void Start () {
		for(int x = 0;x < gameSize;x++){
			for(int y = 0;y < gameSize;y++){
				if(x+y < 8){
					GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
					cube.transform.position = new Vector3(x-gameSize/2, y-gameSize/2, 0);
					if((x+y)%2 == 0)
						cube.renderer.material.color = Color.black;
				}
				else 
					slot = new Vector2(x-gameSize/2,y-gameSize/2);
			}
		}
	transform.position = new Vector3(0,0,-10);
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.touchCount > 0){
			if(Input.touches[0].phase == TouchPhase.Began){
				RaycastHit hit;
				if(Physics.Raycast(camera.ScreenPointToRay(Input.touches[0].position),out hit)){
					
					if(Vector3.Distance(hit.transform.position,slot) == 1){
						var temp = hit.transform.position;
						hit.transform.position = slot;
						slot = temp;
					}
				}
			}
		}
	}
}

this is pretty much keeping in the spirit of the original tutorial in your link… Enjoy!

p.s. please don’t forget to check my answer as accepted ;}

EDIT

pps and may I suggest also this one, unless you have further questions:

http://answers.unity3d.com/questions/365424/how-to-make-camera-to-center-the-dynamically-place.html

(in my above example, we keep (0,0,0) [the world origin] as the center of our grid, by using “gameSize/2” where necessary… )

which is why I simply set the position to (0,0,-10)…