Pinch to Zoom Detection with the Input System - Unity Tutorial

preview_player
Показать описание
Learn how to use the new input system to detect pinching in mobile! I'll also show you how to zoom in and out.

ᐅGet the full Source Code Bundle to my Unity Tutorials 🤓

📥 Get the Source Code 📥

🤝 Support Me 🤝

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won't cost you any money, they will help me fund my development projects while recommending great assets!

😎 Cool Unity Assets 😎

⏱️ Timestamps ⏱️
0:00 Intro
0:19 Install Input System
0:35 Input Action
3:39 Pinch Detection
12:57 Build to Phone
14:44 Outro

💖💖THANK YOU TO ALL MY PATRONS 💖💖

❯❯❯ My Links ❮❮❮
💗 Patreon 💗
💬 Discord Server 💬
🐦 Twitter 🐦
📚 Facebook 📚
🎵 Music 🎵
*Acid Trumpet by Kevin MacLeod
*Airport Lounge by Kevin MacLeod
🎨 Hand in thumbnail 🎨
Made by Walter, my mod in the Discord channel!
👍 Like and Subscribe! 👍

🖥️ Computer Setup 🖥️
*As an Amazon Associate I earn from qualifying purchases.

#madewithunity #gamedev #unity
Рекомендации по теме
Комментарии
Автор

Seems I messed up the build part editing a bit for a couple of seconds, sorry about that!

samyam
Автор

For best results, when declaring previousDistance in the enumerator, have it also default to the same Vector2.distance as in the while(true) loop. Otherwise your zoom will always shrink upon the first touch.

theykeeponrising
Автор

One issue that is missing from the tutorial is the fact that the ZoomDetection coroutine would NOT be canceled if only the FIRST finger is released (it will be canceled only if the SECOND finger or BOTH fingers are released). To fix this - introduce the StopCoroutine() call into the "canceled" event of the Touch#0.TouchContact.

maya_gameworks
Автор

Thank you so much. I watched your other video on Swipe Detection, and for my game it wasn't giving the desired result, since the swipe was detected only after touch ended. I combined the technique in this video and the Swipe one to make a much faster and real time Swipe... Thanks so much for your videos.

vinimaykaul
Автор

Thanks Samyam! Keep pumping out the vids! 👍

scorpionlair
Автор

This worked incredibly well. Thanks for helping me do good at work!

KentWard
Автор

Hello, thank you for your work. I've noticed that you create Actions, but the way of how you use it makes them not the actions but some inner data. I thought that Actions should be like "Jump" or "Zoom" or "Slide", when whoever can subscribe to this actions and gets the same values no matter what controller was performed (if supported by action). But instead you create actions as some value change events and then write script to handle those value changing.

tonymax
Автор

2 Little ways to improve this system aside from what I've seen in the other comments,
Multiply the speed of your zoom in/out by the Mathf.Abs(distance - previousDistance) so that it zooms with the speed of your fingers, feeling more natural
and also adjust the way the camera zooms out depending on where on the screen your fingers are pinching. This is my code for it
Vector3 direction;
direction = + secondaryFingerPosition) / 2).direction;
Vector3 targetPosition = direction * -1 * speed * Mathf.Abs(distance - previousDistance);
cameraTransform.position += targetPosition;
Now with the way I do it, I don't account for Time.Deltatime because whenever I do that, it tends to make everything really really slow and so for the time being I'm just experimenting with things, but if you want to add it, it'd go after Mathf.Abs(distance - previousDistance) :)

Edit: Remove the "* -1" depending on if you're zooming in or out :)

GrepidYT
Автор

Whats is a trick is that if you have two finger on screen, it will fire the secondFinger but if you remove the firstFinger and keep the second, it keeps firing secondFinger.Position. How we can try to solve it?

bisewski
Автор

Excellent video! Helped me out incredibly. I can't thank you enough! <3

TheNash
Автор

On more thing, I think you''ll want to initialise the previous touch distance before you set the zoom start corutine. Other wise the camera will do a weird zoom as soon as the second finger touches the screen.

AM-vrqy
Автор

I have a little question. How do I restrict the first and second touch. So I created a container (type = Image), the class inherits the interface "IPointerDownHandler" and with the method "OnPointerDown ()" I get the first touch position and how do I get the second touch? Your method worked fine too, but I have the problem. That I want to restrict the user and the first and second touch should be triggered within a container and not across the entire screen. 😞
Do you have any ideas? 🤔

siegerhd
Автор

If you do not see "Input Actions", go in Projecet Settings > Player and Set Active Input Handling as "Input System Package (New)" instead of old :)

machineman
Автор

Hello, I have a question, I have a zoom in / out function that works through the mouse scroll and everything is working perfectly, how can I add this pinch function to the input bindigs that I created? Thanks!

ErenBR
Автор

So how would you allow a finger to press multiple buttons at the same time by sliding over them?
The implementation is I am trying to make a mobile joystick for movement on mobile, but there is also a run button above it, and if you slide your finger high enough while also dragging on the move joystick it will engage the running. Please help!

max
Автор

i have iphone and I cant try the "build to Phone" funkcion it when I have all goood and camera move do ?

tadeasnapackach
Автор

I have a question, which is better use for pinch, swipe and press for 3D Object, the new input or lean touch?
-I'mBeginners 🥺 Thanks a lot.

jestoniegummy
Автор

Why whenever you type code, there are options related to Unity ? I dont have it, I using VSCode

idontunderstandnihongoqwq
Автор

Hey im not understanding what the best way is to use swipe detection with the new input system. My implementation works but only if that swiping finger is the only one touching the screen. How do i make my swipe detection able to accept multiple touches. Preferably without using the update function as ive done well so far this project to have very little in update functions

GiggityxGamer
Автор

Hi! Great video, thanks! You show, how can we use pinch to operate with the Camera, but there is another side - we can scale the Object, correct? And in that case we shouldn't move the camera, but should add the script to Object. I wish to make an app, where I could upload the images, then move and scale them. Would you like to make a tutorial something around it? THANKS!

allajohnsonSC
join shbcf.ru