Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Maulik2208 · Dec 26, 2012 at 11:04 AM · androidgameobjecttouchswap

[Edited]Swap position of two Objects for Android on touch

alt text

 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

scpt.jpg (72.1 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Dec 26, 2012 at 11:32 AM 1
Share
 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...

avatar image Maulik2208 · Dec 26, 2012 at 11:43 AM 1
Share

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.....

avatar image Seth-Bergman · Dec 26, 2012 at 12:09 PM 1
Share

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

avatar image Maulik2208 · Dec 27, 2012 at 06:55 AM 0
Share

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

avatar image Maulik2208 · Dec 28, 2012 at 05:44 AM 0
Share

@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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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)...

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Dec 26, 2012 at 03:58 PM 2
Share

Thanks, +1 back at ya!

avatar image Maulik2208 · Dec 27, 2012 at 05:34 AM 1
Share

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????

avatar image Seth-Bergman · Dec 28, 2012 at 11:48 AM 0
Share

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..

avatar image
2

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)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Dec 26, 2012 at 11:37 AM 3
Share

this is only one of SEVERAL debilitating issues here...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game Object On Touch Event on Android-iPhone 2D 4 Answers

Select 2 out of 3 gameobject before doing something. 0 Answers

Drag and Swap two object using touch 0 Answers

[Edited]-Android-swap position on touch 1 Answer

How to select gameobjects on android by touching on them? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges