- Home /
[Edited]-Android-swap position on touch
public GameObject slot,cube;
public Float S_x_temp,S_y_temp;
void Start()
{
slot.transform.position = GridScript.SlotTemp.transform.position;
S_x_temp = slot.transform.position.x;
S_y_temp = slot.transform.position.y;
}
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.transform.position) == 1)
{
Vector3 temp = hit.transform.position;
hit.transform.position = slot.transform.position;
slot.transform.position = temp;
}
}
}
}
}
Hi Guys.....i am trying to swapping the position of two Gameobject when touched on First object..... as shown in picture i have to swap position when a cube nearby the empty slot is touched....Any Help will be much appreciated.....Thanks In Advance....If providing Script Please provide in C#........
Answer by Seth-Bergman · Dec 28, 2012 at 12:15 PM
The problem is probably this line:
if(Vector3.Distance(hit.transform.position,slot.transform.position) == 1)
It works in my example (and the tutorial) because the cubes in both cases are placed EXACTLY 1 unit apart. The way to figure out for sure what is going wrong, at any rate, is by DEBUGGING.
(Since Android Debugging takes a bit of setup, I sometimes simply create a GUI.Label to print the desired value...)
You can use simple tests to figure out what is not working. For example, create a new float variable called distance, then add this line:
if(Physics.Raycast(camera.ScreenPointToRay(Input.touches[0].position),out hit))
{
distance = Vector3.Distance(hit.transform.position,slot.transform.position); //add this line
...etc
Then use a GUI.Label or whatever to print "distance". if it never changes from 0, you probably need colliders on your cubes.. Otherwise, you will see right away that the distance is not exactly equal to 1.