Тёмный

Pinch to Zoom Detection with the Input System - Unity Tutorial 

samyam
Подписаться 83 тыс.
Просмотров 34 тыс.
50% 1

Опубликовано:

 

7 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 113   
@maya_gameworks
@maya_gameworks 2 года назад
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.
@TheAguilacelestial
@TheAguilacelestial 2 года назад
Good point, yes
@adamryason5509
@adamryason5509 7 месяцев назад
Thanks for the tip! For anyone following the tutorial wanting to include this, they can first add a PrimaryTouchContact action to the Input Action. It will be similar to SecondaryTouchAction but with Touch #0. Then they can add the ZoomEnd() method to the canceled action.
@theykeeponrising
@theykeeponrising 3 года назад
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.
@samyam
@samyam 3 года назад
Thanks!
@samyam
@samyam 3 года назад
Seems I messed up the build part editing a bit for a couple of seconds, sorry about that!
@bitcode9612
@bitcode9612 3 года назад
No problem
@bitcode9612
@bitcode9612 3 года назад
Samyam i like your tutorils
@bitcode9612
@bitcode9612 3 года назад
Can you make tutorials on Data structures used for games and make on android Inapp purchases monetizatio and how we can use URP in android games if you do it am very very very thankfull to you or if you not however i love your other stuff
@samyam
@samyam 3 года назад
I have some videos on design patterns ru-vid.com/group/PLKUARkaoYQT22htKCmqYPUTkyYBh4kUif Great suggestions for future videos!
@bitcode9612
@bitcode9612 3 года назад
@@samyam i am waiting for your future videos...
@GrepidYT
@GrepidYT Год назад
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 = Camera.main.ScreenPointToRay((primaryFingerPosition + 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 :)
@vinimaykaul
@vinimaykaul 3 года назад
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.
@samyam
@samyam 3 года назад
Thank you :)
@machine0man
@machine0man 2 года назад
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 :)
@scorpion666lair
@scorpion666lair 3 года назад
Thanks Samyam! Keep pumping out the vids! 👍
@tonymax7503
@tonymax7503 Год назад
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.
@KentWard
@KentWard 3 года назад
This worked incredibly well. Thanks for helping me do good at work!
@lominero5
@lominero5 Год назад
Excellent tutorial! Thank you so much
@TheNash654
@TheNash654 2 года назад
Excellent video! Helped me out incredibly. I can't thank you enough!
@samyam
@samyam 2 года назад
Glad it helped!! 😁
@AM-vr4qy
@AM-vr4qy 3 года назад
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.
@samyam
@samyam 3 года назад
in the coroutine i set the previous zoom distance to 0 before the while loop
@AM-vr4qy
@AM-vr4qy 3 года назад
@@samyam ah sorry my bad. ☺️
@allajohnsonSC
@allajohnsonSC 7 месяцев назад
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!
@bisewski01
@bisewski01 2 месяца назад
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?
@StevenNicholson
@StevenNicholson 2 года назад
These tutorials are awesome @samyam. I've watched a heap now - you've got my subscription. I feel like I'm missing something here with the new input system. It's awesome that I can bucket say a mouse press and a touch press into the same interaction to trigger an event (for example). But having done that, if I want to know where on the screen I touched (or clicked) then I've got to work out query which method I used, and then query that device. Kind of seems counter to the point of bucketing them in the first place. Am I missing something?
@idontunderstandnihongoqwq940
Why whenever you type code, there are options related to Unity ? I dont have it, I using VSCode
@siegerhd340
@siegerhd340 3 года назад
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? 🤔
@samyam
@samyam 3 года назад
You can either use raycasts to cast the finger down position and determine if it touches that object, which I have a video on raycasts here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-JID7YaHAtKA.html Or you could possibly cache the previous on pointer down and release it onpointerup and wait for the next onpointerdown (2nd finger) answers.unity.com/questions/884262/catch-pointer-events-by-multiple-gameobjects.html
@siegerhd340
@siegerhd340 3 года назад
@@samyam thanks for the quick reply and also thanks for the solution 👍
@leonardo6631
@leonardo6631 3 года назад
Great video 😄😄
@jestoniegummy4388
@jestoniegummy4388 2 года назад
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.
@ErenBR562
@ErenBR562 Год назад
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!
@maxg5196
@maxg5196 Год назад
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!
@GiggityxGamer
@GiggityxGamer 2 года назад
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
@mccvargues7792
@mccvargues7792 2 года назад
for some reason my phone only registers the first tap, afterwards tapping the screen never calls started again
@angelinapyankova290
@angelinapyankova290 Год назад
I watched your videos over and over again but I keep wondering if it's possible to use pan, zoom, touch etc., In the same project. And I just don't understand how to do that. I have two scripts: one makes camera zoom in and out(thank you again for your tutorial) and the other one rotates an object. They work separately but don't work togeter. When I switch them both on, only rotation works T-T
@samyam
@samyam Год назад
Yeah it’s possible! Feel free to join our Discord (link in description) and ask for help there (some details on your problem and how you are trying to solve it).
@angelinapyankova290
@angelinapyankova290 Год назад
@@samyam oh, great! I'll join then
@tadeasnapackach4496
@tadeasnapackach4496 2 года назад
i have iphone and I cant try the "build to Phone" funkcion it when I have all goood and camera move do ?
@nishithmannuru101
@nishithmannuru101 Год назад
hey samyam, fantastic video. How do I increase the speed of the zooming in and zooming out?
@lominero5
@lominero5 Год назад
Create a float variable and multiply that times the Time.deltaTime in the Lerp function.
@flightsimguy3897
@flightsimguy3897 Год назад
Why you not use update function i think its easy way and use bool insteat of coroutine?
@samyam
@samyam Год назад
It's cleaner for me than stuffing everything in the Update function and I have more control over when I want to start/stop it
@elritualk
@elritualk Год назад
Hi, great tutorial! I have one question: How to prevent the zoom behaviour when my fingers are touching / interacting with UI buttons? I dont want to zoom in/out when the fingers are interacting with UI things. Thanks.
@lominero5
@lominero5 Год назад
You can add a public bool that gets enabled when ever you don't want zoom. For example: if (!zoomDisabled){ zoomCoroutine = StartCoroutine(ZoomDetection()); }
@angelinapyankova290
@angelinapyankova290 Год назад
Hello again! Watched your video just for the orthographic camera and I can't seem to figure out how to do it. Made a variable "private Camera mainCamera;" then in awake function did this: mainCamera = Camera.main; and in if statement of ZoomDetection did this: Vector3 targetPosition = mainCamera.transform.position; targetPosition.z -= 1; Camera.main.orthographicSize = Vector3.Slerp(mainCamera.transform.position, targetPosition, Time.deltaTime*cameraSpeed); and I get this statement on "slerp" part: Cannot implicitly convert type 'UnityEngine>Vector3' to 'float' What did I do wrong?
@Mireiabalaguer
@Mireiabalaguer 7 месяцев назад
You can't Vector3.Slerp the orthographic Size because it's not a Vector3. You can use Mathf.SmoothStep instead.
@lazyastronaut0
@lazyastronaut0 3 года назад
Thanks
@hungryhedgehog4201
@hungryhedgehog4201 3 года назад
The secondary touch is called for me even though I only touch the scree nwith one finger for some reason, but in another class it detects it as not being pressed.
@samyam
@samyam 3 года назад
Are you using Passthrough or Value in your Action? Try switching from one Action Type to the other to see if that helps.
@lora6938
@lora6938 3 года назад
Hello! How do I make a spot zoom? When the camera zooms in where you put your fingers, and not always in the center
@samyam
@samyam 3 года назад
Once the second finger is pressed down, get the vector of the difference between the first and second finger position, and the middle of that vector will be the center between the two fingers. Then you can move the camera in that direction and zoom in as an example.
@aurelienthazet7510
@aurelienthazet7510 3 года назад
Thanks for this great tutorial ! Is it possible to rotate the camera with the pinch (keeping the zoom/unzoom behaviour) ? And how can I do that easily ? The documentation about the new input system is not really complete 😅
@samyam
@samyam 3 года назад
If you make the gesture uniquely distinguishable it’s possible!
@1hawkgg
@1hawkgg 3 года назад
Good vídeo :)
@cyberfalcon2872
@cyberfalcon2872 3 года назад
What are your thoughts on using Player Input?
@samyam
@samyam 3 года назад
It’s easy to get it set up quickly, but i found that it deletes the stuff I set very frequently in the inspector, ultimately I think the best way is generating the class and making your own input manager
@angelinapyankova290
@angelinapyankova290 2 года назад
Hello! Thank you for your video. I'd like to know if I can make it work with cinemachine, and if it's possible, how can I do it?
@samyam
@samyam 2 года назад
Yes should be the same code mostly, just for the zooming instead of altering the camera directly change the Cinemachine value either the field of view or adjusting the z position of the camera (depends on the type of cinemachine camera you are using)
@angelinapyankova290
@angelinapyankova290 2 года назад
@@samyam Thank you for your answer, I'll try that!
@ChaosResearchParty
@ChaosResearchParty Год назад
What would you recommend for testing / debugging multiple touch in windows?¿ The zoom coroutine never stops for some reason and it is terrible to debug without reproducing it inside unity D:
@samyam
@samyam Год назад
Try using breakpoints and stepping through the code, you can connect your phone to the unity editor and have it use the breakpoints - it's called Remote Debugging ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ICh1ZEaVUjc.html
@l2xu_2000
@l2xu_2000 3 года назад
i gota bunch of errors when my two fingers touching the screen and realsing the screen. Somehow it dont works...
@samyam
@samyam 3 года назад
what are the errors?
@AM-vr4qy
@AM-vr4qy 3 года назад
You say the touch is indexed from #0... but what is primary touch? Isn't that finger 1, then #0 is second finger? Also could you link the documentation that you're using please? I'm not able to find it.
@samyam
@samyam 3 года назад
Primary touch is the first finger that touches the screen, aka Index #0. Second finger that touches the screen is Index #1 Docs docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.InputActionType.html#UnityEngine_InputSystem_InputActionType_PassThrough docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.InputActionType.html#UnityEngine_InputSystem_InputActionType_PassThrough
@AM-vr4qy
@AM-vr4qy 3 года назад
@@samyam thank you! 🙂
@ChaosResearchParty
@ChaosResearchParty Год назад
Is there any extra setting you need to do in order to get the autocomplete functionality you are getting in Vs code? I installed it (along with the .NET sdk, C # package, build tools, .NET extra packages..., unchecked "Omnisharp: Use Modern Net" in the settings in Vscode, and set it up in Unity as default editor + restarted the computer and editor a couple of times) and it doesn't give me autocompletes for On disable and On enable. Maybe this is irrelevant, but it freaks me out whenever I am following a coding tutorial to see that my autocomplete is not working hahaha D:
@samyam
@samyam Год назад
I have a video on it! Set up Visual Studio Code with Unity and INTELLISENSE WORKING 2022 ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ihVAKiJdd40.html
@ChaosResearchParty
@ChaosResearchParty Год назад
@@samyam Yeees, I saw it, thanks a lot
@eduardoholzmann853
@eduardoholzmann853 3 года назад
How can i made to zoom detection higher tan other inputs system?
@samyam
@samyam 3 года назад
Do you mean prioritizing zooming? While it’s zooming you can set a zooming boolean and ignore other inputs while zooming (and under the boolean after the zooming is finished)
@_ciraz_
@_ciraz_ 3 года назад
The zoom doesn't work for me. I attach the created script to an empty GameObject and Build the project to my phone. But when I try to zoom, nothing happens. And I have no errors or anything shown.
@samyam
@samyam 3 года назад
Try attaching your phone to your computer and set it up with the unity debugger I show how to use the Input Debugger here for debugging the input system values: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ICh1ZEaVUjc.html
@_ciraz_
@_ciraz_ 3 года назад
@@samyam I have installed the asset that shows me the console on my phone also, after building to it, but I don't receive any error. I did this tutorial one time and it worked. Now I do it the second time and it doesn't work..
@_ciraz_
@_ciraz_ 3 года назад
@@samyam I will just try and re-make it and see if I still get the errors. Thank you for your time though :)
@samyam
@samyam 3 года назад
Sorry I couldn’t be of more help! It’s hard to tell without seeing the project. If you’d like you can join our Discord and ask there with more details and your code in the #help channel discord.gg/B9bjMxj
@suenandrew1970
@suenandrew1970 2 года назад
I encountered the same problem. Wondering whether you have fixed it. If you have, can you share your solution here? Thanks
@SkyLP
@SkyLP 3 года назад
How does Raycast work with the new Input System?
@samyam
@samyam 3 года назад
I have an example video here Get Object from Mouse Click and Call Functions through Interface - Unity 2020 Tutorial ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-JID7YaHAtKA.html
@danielzupan2516
@danielzupan2516 2 года назад
I just get (Infinity, -Infinity) as position from every touch... wtf?!
@samyam
@samyam 2 года назад
What Input System version are you using? You can also use the Input Debugger to see what values are being returned more easily ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ICh1ZEaVUjc.html
@UODH
@UODH 3 года назад
How can I use the pinch in touch gesture to destroy a gameobject?
@samyam
@samyam 3 года назад
Once the pinch detection is complete you can destroy the game object there. If you want to get the object a finger is on top of you can use raycasts ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-JID7YaHAtKA.html
@UODH
@UODH 3 года назад
@@samyam thanks I really appreciate the help. Subbed!
@UODH
@UODH 3 года назад
@@samyam hey sam. I'm trying to pinch a specific gameobject to destroy. but when I touch the screen with two fingers all gameobjects are destroyed. how can i fix this?
@samyam
@samyam 3 года назад
It’s hard to tell without your code. If you’d like you can share a snippet here or in the #help channel of our discord channel discord.gg/bvQEGk7
@Rico1202
@Rico1202 3 года назад
hello, can you make a tutorial how to make render scale settings for mobile games like genshin impact thx
@samyam
@samyam 3 года назад
I haven’t really played that game, do you mean upload to different resolutions for phones?
@Rico1202
@Rico1202 3 года назад
I mean changing the resolution in mobile games, because I tried to make the settings but I couldn't, and when I played Genshin Impact I saw that in the settings there is a render scale that reduces the render scale in the game so that it improves the game's performance.
@samyam
@samyam 3 года назад
Gotcha, great idea for a video! I’ll put it on my list for a possible future video 🙂
@Rico1202
@Rico1202 3 года назад
@@samyam thx i waiting for ☺️
@Android-Sniper
@Android-Sniper 3 года назад
hello , why new input system?
@samyam
@samyam 3 года назад
Very adaptable to different types of controls and easy to set up and customize! One central place to change your controls no need to go digging through your scripts
@ElCaballoDeUnoCuerno
@ElCaballoDeUnoCuerno 3 года назад
God I wish you would pinch me in
@StickyLabDev
@StickyLabDev 2 года назад
need chinemachine version
@bitcode9612
@bitcode9612 3 года назад
How are you?
@tanmaysalaskar
@tanmaysalaskar 3 года назад
Hey nowadays when we try to install 2020.2.2 editor it says "Interrupted download, unable to download"
@samyam
@samyam 3 года назад
Try unchecking the additional components (install them later). This thread might help forum.unity.com/threads/editor-download-failed-incomplete-or-corrupted-download-file.519098/
@tanmaysalaskar
@tanmaysalaskar 3 года назад
@@samyam yeah after writing my comment i tried the same, i unchecked android and ios support then it worked, but still Thank you for your reply, you are doing a great job❤️❤️👍👍👍
@diliupg
@diliupg 3 года назад
Hi pretty lady, looking forward to you doing a series on ECS, DOTS and the Burst compiler. Cheers! :)
@spatialsparks
@spatialsparks Год назад
Thanks for this tutorial! I have been tinkering around with pinch detection using modifiers in the new input system and your video inspired me to make my own tutorial (in case you are interested: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-LAkQzT_a0zQ.html). Keep up the awesome work!
@bitcode9612
@bitcode9612 3 года назад
Hy
@samyam
@samyam 3 года назад
Hey!
@bitcode9612
@bitcode9612 3 года назад
@@samyam On suggestion for you if want to do please make Vlogs of you and on gaming industry and discuss indie developers specially make your vlogs ...if you can
@rebarius
@rebarius 2 года назад
Too fast 😅and talking also too fast
@samyam
@samyam 2 года назад
I've been working on that on my newer videos :)
@PicturesqueGames
@PicturesqueGames 3 года назад
This is bad. Instead of just using update function you generate several more branches into ienumerator. This is worse than just using update. Much worse in fact. It would've been better have you used a task on a separate thread, but you're not doing that. Branching would look like this > monobehavior > update > checkCooroutines > go into couroutine on yield return > execute > exit coroutine > exit update cycle > exit monobehavior as opposed to monobehavior > enter update > execute > exit update > exit monobehavior
@samyam
@samyam 3 года назад
Thanks for the input. I understand your point, but for this case we are only using one couroutine and not spawning multiple on each frame. Also, since I did not declare an Update function in the script it will not execute the Update. www.jacksondunstan.com/articles/2981
Далее
How to use TOUCH with the NEW Input System in Unity
24:11
The comments on my game were interesting....
9:00
Просмотров 19 тыс.
Pinch to Zoom and Panning on Mobile in Unity
8:00
Просмотров 30 тыс.
Unity's NEW input system in 13 minutes
13:02
Просмотров 18 тыс.
Learning C# In A Week... Otherwise I Fail University
9:04
Strategy Game Camera with Unity's New Input System
41:46