Hello, I’m trying to recreate this Professor Layton puzzle in Unity: https://www.youtube.com/watch?v=JA2AbrI7Z0M
I’m using a Rigidbody2D with a Box Collider 2D to make the object. Here is the code I’m using to make the object drggable using the mouse:
using UnityEngine;
public class DragBlock : MonoBehaviour
{
public Transform block;
Rigidbody2D r;
public float blockDistance;
public Vector3 startingPos;
public Vector3 pos;
public Vector3 distanceBlock;
public bool dragging = false;
public int moveCount;
void Start()
{
r = block.GetComponent<Rigidbody2D>();
block.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
}
void OnMouseDown()
{
startingPos = block.GetComponent<Transform>().position;
Debug.Log(startingPos);
dragging = true;
moveCount ++;
}
void OnMouseDrag()
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
block.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
Vector3 distanceBlock = pos - startingPos;
if(distanceBlock.magnitude < blockDistance && dragging)
{
r.linearVelocity = (pos - block.position) * 10;
}
else
{
r.linearVelocity = Vector3.zero;
}
}
void OnMouseUp()
{
startingPos = block.GetComponent<Transform>().position;
dragging = false;
block.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
r.linearVelocity = Vector3.zero;
}
}
I want to restrict the possible distance while dragging to mimic the “sliding” effect of the original puzzle which only lets you cross one block at a time, so I use an If statement to check if the distance travelled is inferior to the distance limit I want to set.
I then use linearVelocity on the Ridigbody2D, I’m using this method instead of directly changing the object’s position to allow the object to collide and be stopped by other objects, like the other blocks or the play area. I also change the bodyType from Kinematic to Dynamic while dragging an object in order for it to remain in place when the player is letting go of the mouse, while still being stopped by other objects.
I’m having multiple problems with this code:
-
-When clicking, the object moves in order to have the mouse on its center. This wastes some of the distance limit I’ve set and reduces the player’s control. I guess this is caused by my Vector3 pos.
-
The distance limit is fine when moving up or down, but I would need a different distance for going left or right ! Since this limit is calculated from the center and the blue blocks are thinner than they are large, going from the center to one of the side faces is about double the distance from the center and the top or bottom faces.
I don’t really know if this code is the most optimal way to recreate something like this, I am very new to game development in general and programming in particular. I used this because it seemed like the only way to have working colliders, but it’s far from how the game is played on DS. I’m willing to start from scratch if somebody has an idea that could either fix my mess or be closer to the original gameplay.
Thank you so much for reading all that ! Have a great new year