Ak Pasaf
Ak Pasaf

Reputation: 237

Drag and Swap two objects using Touch in Unity

The game has 4 objects with sprite side by side. I want the game to swap the position of two objects when I drag one object and move it towards the second object (dragged object should move to second object's position and the second object should move to dragged object's position). As of now, I can only drag one object and move it towards the second object. However, I am not able to swap their position? Can anyone kindly suggest me on how to do it?

Upvotes: 0

Views: 863

Answers (1)

Attersson
Attersson

Reputation: 4876

The collision can be handled with a collider2d.

At collision time, when the objects touch or even overlap, they would enter a non deterministic state where they could keep swapping.

I suppose you want to save the original position of the moved object and when this object collides with another swappable object, release the moved object from touch and set its transform coordinates to the touched object, then move (once again by setting transform coords) the touched object to the starting position of the moved object.

Upvotes: 1

Related Questions