Pinch and Scroll to Move and Zoom in Unity for Mobile Games

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ย. 2024
  • This time I will go back to my roots and give you guys a deeper look in mobile development with unity. Besides first or third person there are possibilities to use top down cameras.
    I will show how pinch, scrool and zoom can be translated into movement, rotation and zoom in Unity. This all works like in google maps or any photos app. The position you are touching the content will stick to your finger. You can than move them together to in the opposite direction.
    The script is ready to go.
    Script: gist.github.co...
    Unity Remote: docs.unity3d.c...
    #ditzelgames #unity #gamedev

ความคิดเห็น • 102

  • @Kudoshi
    @Kudoshi 3 ปีที่แล้ว +10

    This is by far the best tutorial I have ever seen. Really love how you take your time to explain with diagrams and markers.
    You even explain what each code does with the diagrams. Helps a lot to understand the concept behind it.
    Definitely cannot be compared to tutorials where they just chuck the code at your face and never explains it. This is just pure gold content.

  • @ankitmehrotra6104
    @ankitmehrotra6104 4 ปีที่แล้ว +1

    I searched man Tutorials on Pinch and Zoom . But this Tutorial was the Only one , which helped me :)

  • @loveparfum
    @loveparfum 4 หลายเดือนก่อน +1

    it is indeed a very good tutorial. Thank you, you helped me a lot !

  • @tvguideondemand
    @tvguideondemand 4 ปีที่แล้ว +2

    This is without a doubt the best tutorial. The only thing missing is how to orbit the camera around an object. Which I can probably figure out.

  • @hnguyen5670
    @hnguyen5670 3 ปีที่แล้ว +8

    I've been searching for a tutorial on mobile touch navigation. I found your tutorial is the best one with the practical explanations that you drew on the screen. Could you create a new mobile touch tutorial with the new Unity Input System?
    Thanks for the wonderful tutorial. I liked and subscribed to your channel.

  • @jaydekemsley2594
    @jaydekemsley2594 2 ปีที่แล้ว

    Wow! Watched the tutorial, plugged the code in, hit play. Perfect out of the box!
    Thank you!

  • @bucherbernd
    @bucherbernd 3 ปีที่แล้ว +1

    you saved me a lot of time, thank you, the script works very well

  • @cho.gath789
    @cho.gath789 3 ปีที่แล้ว

    I watched 6 youtube tutorials hoping to achieve this. And your video is the only one that proved useful! :D

  • @merleatmo
    @merleatmo 5 ปีที่แล้ว +1

    Excellent code ...Tested and works smoothly. I added a UI button to call the public bool variable Rotate. So i can tested on my device and the rotation is very smooth. Thanks a lot for this tutorial.

  • @zekiunat
    @zekiunat ปีที่แล้ว

    This is still working fine for Unity 2021.3.11f1. Thank you very much!

  • @SociopathDev
    @SociopathDev 3 ปีที่แล้ว +5

    Anyone have been able to limit the scroll in certain boundaries? I was able to limit the zoom alredy, then I tryed to set boundaries without without rotation and worjks fine, but i'm not able while rotating, any solution?

  • @moosesnWoop
    @moosesnWoop 3 ปีที่แล้ว +1

    This is excellent. Thank you so much, I'm going through the code now and will test it out shortly.

  • @whiteshadow3451
    @whiteshadow3451 3 ปีที่แล้ว +1

    ah! finally, i found it.... thanks to you 💓💓💓

  • @Kudoshi
    @Kudoshi 3 ปีที่แล้ว +8

    To those who wants to :
    - Restrict zoom in and out
    - Adjust camera pan speed
    Check out the fork that I made in the github repository. I added a few simple lines of code to do the above functionality.

    • @Kudoshi
      @Kudoshi 3 ปีที่แล้ว

      @Arthur Alves Check in this yt description. There's a link to his github repository. Then under the fork tab check the one i forked out.

    • @mainaody3997
      @mainaody3997 3 ปีที่แล้ว

      Thank you

    • @hakanozek708
      @hakanozek708 3 ปีที่แล้ว +1

      hey man, I want to limit the camera movement in the code you wrote, but I couldn't integrate it into the code, can you help?
      Camera.transform.position = new Vector3(
      Mathf.Clamp(Camera.transform.position.x, -100f, 100f),
      Mathf.Clamp(Camera.transform.position.y, 100f, 100f),
      Mathf.Clamp(Camera.transform.position.z, -100f, 100f));

    • @MasonTheBigBoy
      @MasonTheBigBoy 2 ปีที่แล้ว

      @@hakanozek708 did you ever figure this out? If so please help me

  • @khairolhazeeq5426
    @khairolhazeeq5426 3 ปีที่แล้ว

    Thank you so much. Very good tutorial! You've got a new subscriber!

  • @kora_lee
    @kora_lee ปีที่แล้ว

    9:27 Thank you! A very useful video :)

  • @manildayma
    @manildayma 4 ปีที่แล้ว +1

    I noticed that the pinch and scroll take away the camera to out of bounds. This glitch makes the map out sight by totally zooming in or zooming out by the user, hence creating a situation to restart the app. If there is an add-on to prevent moving the camera out of specific area then it would have been a great script. The scroll and pinch features works well, though.
    Also, angle shifting should also be there.

    • @christopherlilja7423
      @christopherlilja7423 4 ปีที่แล้ว +2

      Honestly Manil, yes this is an issue - However.. i think most of those coming to this tutorial, myself included are less inclined to just rip the whole script. I myself have never used pinch/drag(other than UI Elements) features in games as ive always tended to stay away from mobile. So this work is Extremly useful for this purpose.
      That being said 99% of the "hard work" and thinking has been done for you.... Honestly writing a tiny little script to work with whatever your limitations are intended to be is so beyond simple its difficult to understand your concirn.
      for zoom.
      //Move cam amount the mid ray
      Camera.transform.position = Vector3.LerpUnclamped(pos1, Camera.transform.position, 1 / zoom);
      This line appears to be responsible for the zooming at a glance.
      So all you would need to do is create a new public variable called "zoomLimit" and create an if statement around the above line. Ignoring that line if the camera height is greater than the zoom level allowed.
      then an else to that if to set the camera height back under the allowed zoom level, re-enabling the pinch code.
      Do the same for a "lower limit" and there you go. One more variable, and a couple of if/else statements = fixed.
      For the camera out of bounds issue, that entirely depends on how your game works, simplist thing that comes to mind is a "Scroll back" feature based on an empty/other object in the scene. If your distance is greater than a certain ammount to that object. Force the camera to move toward that object until your within the "bounds" again.
      Happy coding! And for your own sake try writing more independant code than skimming whatever scripts you can off the internet. Nothing will ever work "exactly how you need it to" and will only cause you more headaches in the long run, just be creative with your solutions.

  • @Blooderstokizz
    @Blooderstokizz 3 ปีที่แล้ว +1

    How to make it smooth ? After i leave the touch the camera to continue moving some extra distance and slowly stop. Please someone help

  • @JM-dc5rn
    @JM-dc5rn 4 ปีที่แล้ว +3

    If anyone wants just the Screen panning in its most simple form:
    public float panSmoothing;
    void Update()
    {
    HandleInput();
    }
    public void HandleInput()
    {
    if(Input.touchCount > 0)
    {
    Touch touch = Input.GetTouch(0);
    if(touch.phase == TouchPhase.Moved)
    {
    Vector2 pos = touch.deltaPosition;
    Vector3 position = new Vector3(pos.x / panSmoothing, 0, pos.y / panSmoothing);
    this.transform.position -= position;
    }
    }
    }

  • @nimrsawafta1682
    @nimrsawafta1682 2 ปีที่แล้ว

    You saved my weekend

  • @ivans8713
    @ivans8713 ปีที่แล้ว

    for me rotation doesnt work for some reason. i have no clue what stuff in those codes are, most of them are completely new to me but one thing is important: i copied your code and it works. thank you so much what else to say

  • @christopherbousquet-jette117
    @christopherbousquet-jette117 3 ปีที่แล้ว

    Nice code :)!
    Best regards,
    Christopher

  • @_Garm_
    @_Garm_ 5 ปีที่แล้ว +1

    great tutorial, thank you! :)

  • @LukeClemens
    @LukeClemens 4 ปีที่แล้ว

    Awesome algorithms! Thanks!!

  • @playedugame
    @playedugame 3 ปีที่แล้ว

    Great job Sir!

  • @lora6938
    @lora6938 3 ปีที่แล้ว +1

    Can you PLEASE 🙏
    also but for Orthographic camera ?

  • @babdie
    @babdie 2 ปีที่แล้ว

    Can i ask , why do we need Plane.Raycast , is it because ur terrain doesnt have a collider for Screentoworlpointray to hit?

  • @roberthubinak5626
    @roberthubinak5626 3 ปีที่แล้ว

    Thank you very much man 😊

  • @sn3lling
    @sn3lling 4 ปีที่แล้ว

    Thank this was a massive help

  • @moheyaldeenalmardiny82
    @moheyaldeenalmardiny82 3 ปีที่แล้ว

    you are a life saver

  • @DominicPlatten
    @DominicPlatten 3 ปีที่แล้ว +1

    Where would youintegrate max and min Range on the zoom?

  • @gramstalex
    @gramstalex 4 ปีที่แล้ว

    Great job man!

  • @huluhulu7218
    @huluhulu7218 3 ปีที่แล้ว

    thank you for sharing !!

  • @fluorescente
    @fluorescente 2 ปีที่แล้ว

    well truth is because of the IF IOS ANDROID thingy, I don't even have the inspector fields to attach the camera, so it doesn't work at all

  • @Blooderstokizz
    @Blooderstokizz 3 ปีที่แล้ว +1

    Hey man very good script. I took the script modified and made it to work the way I want. I just need one thing to fix . How can I change zoom speed ? Its obviously in this line : Camera.transform.position = Vector3.LerpUnclamped((pos1 + pos2) / 2f, Camera.transform.position, (1 / zoom)); I tried to change parameters but nothing worked : /

    • @Blooderstokizz
      @Blooderstokizz 3 ปีที่แล้ว

      (pos1+pos2) / 2f is modified so when i zoom the camera goes between the 2 touch inputs

  • @ryongchoi9992
    @ryongchoi9992 2 ปีที่แล้ว

    It works perfectly, but I cannot use any canvas UI. is there any way to make finger tap works like "Click" on Cavas?

  • @NerdConfederation
    @NerdConfederation ปีที่แล้ว

    Would've been real nice to note this only works in projection, not ortho... :/

  • @user-pg2fo5gy8c
    @user-pg2fo5gy8c ปีที่แล้ว +1

    Any updates for new input system?

  • @skudgee2
    @skudgee2 2 ปีที่แล้ว

    Best one !!

  • @ibanlopez96
    @ibanlopez96 3 ปีที่แล้ว

    Thank you, the tutorial has helped me a lot. But, can you explain how to move the camera to a certain point when you click to one tree, keeping the zoom? I mean, pointing the tree on the middle of the screen. I've tried to translate and lerp but it doesn't work. What I managed is to detect the tree using ScreenPointToRay and RaycastHit. Thank you.

  • @tvguideondemand
    @tvguideondemand 4 ปีที่แล้ว

    On Google Maps, if you drag across the screen with two fingers and maintain the same distance, the camera doesn't zoom in or out. Do you know how to replicate this with this code?

  • @kiethuynh744
    @kiethuynh744 3 ปีที่แล้ว

    Love!

  • @bounames
    @bounames 4 ปีที่แล้ว

    thank you kind sir!

  • @juannuestro783
    @juannuestro783 3 ปีที่แล้ว

    how did you get that rotate and camera option on script?

  • @nicolaskamien1825
    @nicolaskamien1825 4 ปีที่แล้ว +1

    Hello. What would you do to limit the zoom and translation within the map? (example: max aproach is the tree level and max zoom out is four times that)
    I have tried everything and I could not get it to work. Is limiting the y axis a good approach?
    Would really appreciate a response. Thanks!!

    • @christopherlilja7423
      @christopherlilja7423 4 ปีที่แล้ว +1

      I responded to someone earlier with something simalar, so im just going to copy and paste this here
      Honestly Manil, yes this is an issue - However.. i think most of those coming to this tutorial, myself included are less inclined to just rip the whole script. I myself have never used pinch/drag(other than UI Elements) features in games as ive always tended to stay away from mobile. So this work is Extremly useful for this purpose.
      That being said 99% of the "hard work" and thinking has been done for you.... Honestly writing a tiny little script to work with whatever your limitations are intended to be is so beyond simple its difficult to understand your concirn.
      for zoom.
      //Move cam amount the mid ray
      Camera.transform.position = Vector3.LerpUnclamped(pos1, Camera.transform.position, 1 / zoom);
      This line appears to be responsible for the zooming at a glance.
      So all you would need to do is create a new public variable called "zoomLimit" and create an if statement around the above line. Ignoring that line if the camera height is greater than the zoom level allowed.
      then an else to that if to set the camera height back under the allowed zoom level, re-enabling the pinch code.
      Do the same for a "lower limit" and there you go. One more variable, and a couple of if/else statements = fixed.
      For the camera out of bounds issue, that entirely depends on how your game works, simplist thing that comes to mind is a "Scroll back" feature based on an empty/other object in the scene. If your distance is greater than a certain ammount to that object. Force the camera to move toward that object until your within the "bounds" again.
      Happy coding! And for your own sake try writing more independant code than skimming whatever scripts you can off the internet. Nothing will ever work "exactly how you need it to" and will only cause you more headaches in the long run, just be creative with your solutions.

    • @christopherlilja7423
      @christopherlilja7423 4 ปีที่แล้ว +1

      Feel free to drop me a message privately if still struggling.

    • @nicolaskamien1825
      @nicolaskamien1825 4 ปีที่แล้ว +1

      ​@@christopherlilja7423 Thanks for your reply and your elaborate answer.
      I have already tried exactly what you proposed in your response. By doing that, the screen goes crazy, trying to fix the screen by every frame (because it is in the update method). I haven't figured out a way to do that limitation smoothly, so I'm studying and trying to find a way to work around it.
      Still interested in suggestions! Thanks!

    • @christopherlilja7423
      @christopherlilja7423 4 ปีที่แล้ว +1

      @@nicolaskamien1825 hey Nicolas!
      If your looking for a smooth transition, back to the "play area" you need to treat the camera movement more like... player movement.
      So when you move a player to xyz, you don't just set its position... i think a working method would be something like this. Your camera is composed of two objects. One that "follows" the cameras position exactly and the other being the camera.
      Once you have that working. You write a condition in the follow camera script to not allow for the following object to exit your set bounds. This will result in that object basically being "stuck" right at the edge of where the camera is not supposed to be allowed to exit. Giving you a good reference point.
      Then in your camera dragging code, your going to add the function in/use the same function as you did earlier to detect that the camera has gone out of bounds also.
      Then you have the ability to force the camera to move based on a speed value, simalar to:
      gameObject.transform.position = new Vector2 (transform.position.x + (h * speed), transform.position.y + (v * speed));
      Although obviously you will need to use a vector3 as this is a 3d game.
      Speed can be derived from a default set speed all the time, then if i were you. I would probably increase that speed variable based on distance, not allowing it to ever be lower than default. But 1x when 1 meter out. 2x when 2 meters out. And so on.
      This will result in a quick "snap" back to position when you stop manually controlling the camera, that will slow down the futher you reach your out of bounds edge/the following object!
      If your still struggling let me know! Im going to have to write this same exact process anyway and i can just throw some code together earlier than planned and share it. In fact, i will see about doing that right now. Ive got some time to kill.
      Happy coding!
      Chris

    • @christopherlilja7423
      @christopherlilja7423 4 ปีที่แล้ว +1

      @@nicolaskamien1825 Hi Nicolas!
      So I managed to find a secondary script that i adapted to fit what i needed for myself.
      pastebin.com/9u2LXdcN - Link here.
      it uses Mathf.Clamp to bind to a set of coordinates EX X[-10,10] Y[-20,40], as well as zoom.
      If you need the rotate function included (I didn't) it shouldn't be too difficult to adapt what you can see in this script to fit the one provided, and roll it into the 2 finger switch case. It actually is a far easier implimentation than the suggestion i listed above. But doesn't have a "Smooth panning" effect to return to the bounds of the camera, Instead the camera is physically unable to pan outside of the zone you dictate in the public arrays, and as its "Clamped" that float is incapable of going higher than a certain #. Fixing the stuttering/Crazy camera issue :')
      Either way, adapt the clamping into this script, or pull the rotation from this one.
      Hope this helps!

  • @michaelheyn3255
    @michaelheyn3255 4 ปีที่แล้ว

    Thanks!

  • @leabarrionuevo
    @leabarrionuevo 4 ปีที่แล้ว

    Hello how are you doing? if I want to move the object (not the camera as in this case) how can I do that?

  • @fluorescente
    @fluorescente 2 ปีที่แล้ว

    can we make the zoom use size with ortho cameras?

  • @sujint1240
    @sujint1240 5 ปีที่แล้ว +2

    can I use Clamp here?? to limit my Camera Movement ?

    • @umairmalik6480
      @umairmalik6480 4 ปีที่แล้ว +1

      Did you use clamp then?

    • @salihbalkis72
      @salihbalkis72 3 ปีที่แล้ว

      Use clamp with a Lerp to smooth. It works

    • @hakanozek708
      @hakanozek708 3 ปีที่แล้ว

      hey dude, can you write how did you do it?

  • @mishahere
    @mishahere 2 ปีที่แล้ว

    When I create a new script there is no camera slot. I'm using Unity 2020.3.14. Any ideas ?

  • @pilotrl2709
    @pilotrl2709 3 ปีที่แล้ว

    Hello
    How can I Rotate only the camera?
    Thanks

  • @salihbalkis72
    @salihbalkis72 3 ปีที่แล้ว

    When I touch the top of my phone's screen to translate the camera, it starts to freak out. It suddenly move faster and I can't control when it comes to touch the top of screen. Is there any way to fix this?

    • @salihbalkis72
      @salihbalkis72 3 ปีที่แล้ว

      I found out something. When I touch the bottom of the screen, movement is slow. When I touch the centerof the screen, movement is properly fast. When I touch the top of the screen, movement is way much faster.

    • @robinwood4728
      @robinwood4728 2 ปีที่แล้ว

      @@salihbalkis72 Same.
      Did you find a solution?

  • @dimirionpro8451
    @dimirionpro8451 4 ปีที่แล้ว

    How to make it workable for both PC and Android?

  • @hamidrezashahbazi9620
    @hamidrezashahbazi9620 5 ปีที่แล้ว +3

    OMG

  • @tommans3125
    @tommans3125 4 ปีที่แล้ว

    Can you make a tutorial on how to create a script that allows you to grab and throw object in unity for android/ios?

  • @modiftoys471
    @modiftoys471 4 ปีที่แล้ว

    Do you have tutorial drag and drop object like COC? i need that now

    • @christopherlilja7423
      @christopherlilja7423 4 ปีที่แล้ว +1

      Dragging and dropping an object is easy.
      Detect mouse click + Hold
      Get mouse world position when Clicked and held.
      Move object to position stated above.
      Think of it less like dragging an object, more like.. an object following the mouse position and its far easier to wrap your head around.

  • @tamasszucs5604
    @tamasszucs5604 5 ปีที่แล้ว

    What if its a Terrain and not a Plane?

  • @justadevlog1667
    @justadevlog1667 3 ปีที่แล้ว

    how to i slow down speed of rotation and cameramovement?

    • @mohamedkaram5668
      @mohamedkaram5668 3 ปีที่แล้ว

      I used vector3.MoveTowards to control the camere movement where I consider the 3rd parameter of the function a speed modifier. The rotation is not working for me yet though

  • @TheJAX9585
    @TheJAX9585 4 ปีที่แล้ว

    how to pan in opposite direction.

  • @ClaireBodley
    @ClaireBodley 4 ปีที่แล้ว +2

    Has anyone been able to limit the panning so that the camera view does not go off the edges of the plane?

    • @DRagnMastrN7
      @DRagnMastrN7 4 ปีที่แล้ว

      Just set limits for the camera.

    • @priyanshneel2284
      @priyanshneel2284 4 ปีที่แล้ว

      @@DRagnMastrN7 how????

    • @DRagnMastrN7
      @DRagnMastrN7 4 ปีที่แล้ว

      @@priyanshneel2284 It depends to you needs, haha. Via code use something like (camera.position.x > limit) camera.position.x = limit;

    • @SociopathDev
      @SociopathDev 3 ปีที่แล้ว

      @@DRagnMastrN7 ye but this wont work if u rotate the camera :S

  • @hotclaw
    @hotclaw 4 ปีที่แล้ว

    Hi. Where do you create the plane? It's not in the posted gist nor shown in the video.

  • @jhanagotama9189
    @jhanagotama9189 3 ปีที่แล้ว

    Maksihh gan

    • @terdengarwe6957
      @terdengarwe6957 3 ปีที่แล้ว

      work gan?... di ane ko dizoom trus blackscreen ya

  • @IfThenElseMusic
    @IfThenElseMusic 5 ปีที่แล้ว +1

    can you provide the code in text?

    • @DitzelGames
      @DitzelGames  5 ปีที่แล้ว +1

      It's in the description

    • @IfThenElseMusic
      @IfThenElseMusic 5 ปีที่แล้ว +1

      Thanks, i support you for mobile game developer

    • @IfThenElseMusic
      @IfThenElseMusic 5 ปีที่แล้ว +2

      It is helping me a lot for developing a game name FPL by fantasy developers

    • @IfThenElseMusic
      @IfThenElseMusic 4 ปีที่แล้ว +1

      @@umairmalik6480 Why? What Happened?

    • @umairmalik6480
      @umairmalik6480 4 ปีที่แล้ว +1

      When you use this script in your game it works perfectly for you?

  • @mechtarin
    @mechtarin 3 ปีที่แล้ว

    does not work and what is PLANE not good

  • @felixborg8257
    @felixborg8257 3 ปีที่แล้ว

    Wotate

  • @omarsagr
    @omarsagr 3 ปีที่แล้ว

    Thanks a lot bro 👍🏼