- Home /
How to swap textures?
Hi. I am making a puzzle game and I would like that id the player moves an object on the other thex would swap positions. Can I do it in one code or shall I wrote an other so when something is palced there it would get the place of the other object? If I can write it in one code, how can I make that if something is moved to an other object they swap positions? I guess with GetPosition. And how can I set that from where shall it get the new position? (So the player don't have to move exactly on the object just near to it)
Thanks for your help.
Can semobody help? Something where I shouldshall start from?
Take some time to write properly. Doesn't look like english is a problem for you, this is just you typing quickly and not giving a shit, annoying to read. Farewell to you sir.
@Draghou, your question is super difficult to follow. Please clarify.
Answer by robertbu · May 08, 2013 at 07:24 PM
There are a number of related technical questions here. Without your code, it is difficult to give you advice. And as others have pointed out, your question is difficult to read.
The first challenge is the dragging and dropping. Do you have that solved? If not search this list and you will find many answers. The ones involving OnMouseDown(), OnMouseDrag(), OnMouseUp() will be easiest to implement, but the Physics.Raycast() solutions will work as well.
The second challenge is moving pieces to a specific location. Games like the one I think you are making look best if the pieces move to their new positions over time. Typically I like to make each piece know where it "home position" is and then if I need to move it just change the "home position" and let the piece do its own work. Look at Vector3.Lerp()as well as the numerous posts on this list about moving things over time.
To detect when one piece is over the other, you will need to Raycast. Take a look at Physics.Raycast() plus any number of posts on raycasting on this list.
If you've implement 1 - 3 above, you can swap pieces by simply swapping their home positions and allow the pieces to fly to their new positions.
Note your question asks about swapping textures. This will produce an instant change between the two pieces. That is one way to approach the problem, but unless you have a specific need in your game to swap textures, you could just swap the positions of the two pieces:
Vector3 v3T = piece1.transform.position;
piece1.transform.position = piece2.transform.position;
piece2.transform.position = v3T;
Assuming the mesh for the two pieces is the same, this swap of position will look like a swap of textures.