- Home /
[Edited]Swap position of two Objects for Android on touch
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
Vector2 touchDeltaPosition = Input.GetTouch(0).position;
(not deltaPosition.. you want the actual position..)
if(Vector3.Distance(touchDeltaPosition,slot.transform.position) == 1)
ins$$anonymous$$d say
if(Vector3.Distance(touchDeltaPosition,slot.transform.position) <= 40)
use
NOTE: a distance of 1 is literally an adjacent pixel very unlikely you could get that accurate if you try
FURTHER$$anonymous$$ORE, you are giving your objects a position.z of 10, but TOUCH INPUT is a Vector2 (either position or deltaPosition), which means the closest you could possibly get is 10 (if you tap the EXACT pixel)!!
(because you are implicitly converting a Vector2 to Vector3, the z value is ZERO)
beyond the numerous errors, I'm still a bit unclear on what you are after.. but probably also you don't want "TouchPhase.$$anonymous$$oved", unless you are trying to touch&drag the object..
If you are more clear I can help...
1)i am just trying to swap the position of two game objects means if i touch the 1st gameobject cube then it should interchange it's position with 2nd gameobject slot..... for more info https://www.youtube.com/watch?v=sSteq3uzICI go to this link.....i am trying to doing the same with touch on android.....
I can see you've been at this for a while :)
just give me a bit, I'll be happy to help you with an example ;D
please have a look at the image uploaded on device your script works fine but can't figure out why it is not working on $$anonymous$$e????? for creating grid i have another script and in this script i am just trying to move the cube and the slot.......and one more thing ....my each cube is having this script......as per the tutorial i am following which i have mentioned
@Seth Bergman don't know but this one won't works with $$anonymous$$e cubes......could you please convert your script in C# because i think
if(Vector3.Distance(hit.transform.position,slot) == 1){ var temp = hit.transform.position; hit.transform.position = slot; slot = temp; }
above part is in java script......and i am not good with java script.....so please help me with that......Thanks a lot in advance
Answer by Seth-Bergman · Dec 26, 2012 at 02:22 PM
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.
:)
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)...
if(Vector3.Distance(hit.transform.position,slot) == 1){ var temp = hit.transform.position; hit.transform.position = slot; slot = temp;
one small question was there i think this is javascript so will you please convert it to c# because i don't knoe javascript......And how can i use this script in my project?????means should i have to just copy and paste the update function to replace my update function????
it's not javascript, this is a valid c# script, I have tested it.
(the var keyword is in fact valid in c#, it declares a generic var)
this script is a fully self-contained example.. it generates its own cubes. all you have to do is attach it to the main camera of a NEW scene
the reason I say new scene is simply because I haven't accounted for all of the possible changes you may have made to the main camera of an existing scene..
obviously, for this to work with your own grid, some modification would be required... But it shouldn't be TOO difficult.. The problem in all likelihood is the part:
if(Vector3.Distance(hit.transform.position,slot) == 1)....
it works in my example (and, indeed, in the tutorial you linked) because the cubes are VERY PRECISELY set exactly 1 unit apart.. They are EXACTLY 1x1x1 unit by default... if you were to set up your grid similarly, it would work with the above code (just the Update function). (Your boxes also need a box collider, and of course you would need to set the var "slot" to the appropriate value)
an alternative would be simply to attach your own material to the generated cubes, which would probably be easier in terms of modification...
I didn't have time before, but I will comment my example to make it easier to follow..
Answer by Shrandis · Dec 26, 2012 at 11:13 AM
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
if((Vector3.Distance(touchDeltaPosition,slot.transform.position) <= 1)
this is only one of SEVERAL debilitating issues here...