Restrict 2D object dragging to the screen in Unity

  Рет қаралды 1,301

Firnox

Firnox

Күн бұрын

Пікірлер: 5
@SpeedLuffy
@SpeedLuffy Жыл бұрын
i just subscribed you and became your 500th subscriber. Congrats on 500 subs. i have one doubt. when i drag the hexagon object over sqaure object then im having object overlapping issue. to avoid object overlapping could you suggest something or make a turorial on it? that would a great help. thank you
@FirnoxGames
@FirnoxGames Жыл бұрын
Thank-you so much for being my 500th subscriber. To say I didn't spend the day checking would be lying :) This is a bit tricky, normally people instantiate all their objects with different z or sort order to ensure they are never at the same level, which can mean the renderer is not sure which to do. This works for dragging, when you pick up an object you just set the value to make sure it's on top of everything. The question is what to do when you drop it. If you return it to it's original value then it could end up beneath the object you've just put it on, that wouldn't have the overlapping issue, but it's also not great. The simplest solution to this is to retain the z/sort order of the current top piece then when you drop set it to be above that (and update the top) that will always mean when you put a piece down it'll be above the others.
@AnotherWorld-r5z
@AnotherWorld-r5z 9 ай бұрын
How to implement this to your jigsaw puzzle series?
@AnotherWorld-r5z
@AnotherWorld-r5z 9 ай бұрын
i tried to add this to puzzle game, but now its not detecting clicks on puzzle selection on the grid, please help
@FirnoxGames
@FirnoxGames 9 ай бұрын
This video uses sprites, so I got the bounds of the sprite using the bounds on the SpriteRendere. However in the Jigsaw puzzle I created the pieces using Textures so it doesn't work as a direct copy/paste. However, it is straightforward to add all you need to do is change the dragging code within the update section. As before we get the screen bounds (same as this video). Then to limit the movement, we already have the piece sizes in the global width and height variables, we just need to scale them with respect to the gameholder (same as when we scattered the pieces) and divide by two to get half the width. Then we use clamp as in this video to limit the movement. Hope this helps: // Set the dragged piece position to the position of the mouse. if (draggingPiece) { Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //newPosition.z = draggingPiece.position.z; newPosition += offset; // Find the screen bounds in world coordinates. Vector3 topRight = Camera.main.ViewportToWorldPoint(Vector3.one); Vector3 bottomLeft = Camera.main.ViewportToWorldPoint(Vector3.zero); // Limit to the screen float halfWidth = width * gameHolder.localScale.x / 2; float halfHeight = height * gameHolder.localScale.y / 2; newPosition.x = Mathf.Clamp(newPosition.x, bottomLeft.x + halfWidth, topRight.x - halfWidth); newPosition.y = Mathf.Clamp(newPosition.y, bottomLeft.y + halfHeight, topRight.y - halfHeight); draggingPiece.position = newPosition; }
Let's Create: Minesweeper in Unity
20:41
Firnox
Рет қаралды 2,7 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 15 МЛН
[BEFORE vs AFTER] Incredibox Sprunki - Freaky Song
00:15
Horror Skunx 2
Рет қаралды 20 МЛН
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 56 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 54 МЛН
Unity Basics Episode 6: Your First Game!
19:10
Damm Labs
Рет қаралды 97
Enemy Patrol/Wander AI Unity SIMPLE & EFFECTIVE CODE! Tutorial
10:00
How to Host Unity a Webgl Game on Github for Free
3:57
Max O'Didily
Рет қаралды 10 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 15 МЛН