Hi All!
I’ve pretty familiar with Unity but never touched 2D puzzle game world so I’d like to know what is the recommended way to do it?
for example - see game on picture below - I mean:
-
is there any guide to implement drag-n-drop (mobile\touch) or click source-then target approach?
-
should letters and their placeholders be gameobjects with colliders so that I check OnTriggerEnter event and find out if they match or that should be done in some other way?
-
should they actually be real gameobjects or canvas UI-objects are recommended (why)?
maybe there is a good learning project from Unity to start with?
picture source:
There are definitely word game tutorials for Unity, of various types. I would start there.
Also, most of what you want to do above is covered in tutorials for drag-and-drop inventory UI systems.
The first thing I would do is try to drag tiles around with your finger
Next make them “latch into” special cells on screen that you pre-locate (such as the row above)
Third read the letter off the tile and display it in the fixed cell when it drops into place
Now you have all the UI, it’s just making and connecting the game logic, which is dependent on your design.
1 Like
Found pretty good and very quick tutorial for drag-n-drop puzzle (7 minutes) - share with you:
Warning: tutor uses one script per puzzle item (BearScript, HareScript, etc) - I would recommend to use SINGLE one
- code from manual (slightly changed, worked for me):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PuzzleObj : MonoBehaviour
{//tutorial - https://www.youtube.com/watch?v=7HEjCEncezs
public Transform targetPosition;//target posion to place to
private Vector2 initialPosition;//to return to
private float deltaX, deltaY;
public bool locked; //static? //set to true when placed to correct location
void Start()
{//remember where to return to
initialPosition = transform.position;
}
void Update()
{
if(Input.touchCount > 0 && !locked)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
switch(touch.phase)
{
case TouchPhase.Began:
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos) )
{
deltaX = touchPos.x - transform.position.x;
deltaY = touchPos.y - transform.position.y;
}
break;
case TouchPhase.Moved:
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos))
{
transform.position = new Vector2(touchPos.x - deltaX, touchPos.y - deltaY);
}
break;
case TouchPhase.Ended:
if (Mathf.Abs(transform.position.x - targetPosition.position.x) <= 0.5f &&
Mathf.Abs(transform.position.y - targetPosition.position.y) <= 0.5f)
{
transform.position = targetPosition.position;
locked = true;
}
else
{
transform.position = initialPosition;
}
break;
}
}
}
}
and a question: why the tutor recommended to make public bool locked; static?