Unity Twin Stick Controller Tutorial (Gamepad and Keyboard) | Top Down Shooter Controls

Поделиться
HTML-код
  • Опубликовано: 19 ноя 2024

Комментарии • 107

  • @jacklee8705
    @jacklee8705 2 года назад

    Been struggling to understand the new Unity Input system until this video. Big help thanks

  • @GazzTino
    @GazzTino Год назад +16

    Thanks this video was extremly helpful to me :D I had to change a little bit the rotation with the right stick because I was working on a 2D project so is not exactly the same. Here is the code if someone is interested
    if (Mathf.Abs(aimInputVector.x) > controllerDeadzone || Mathf.Abs(aimInputVector.y) > controllerDeadzone)
    {
    Vector2 playerDirection = new Vector2(aimInputVector.x, aimInputVector.y);
    if (playerDirection.sqrMagnitude > 0.0f)
    {
    Quaternion newRotation = Quaternion.Euler(0f, 0f, Mathf.Atan2(playerDirection.y, playerDirection.x) * Mathf.Rad2Deg - 90f);
    transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, gamepadRotateSmoothing * Time.deltaTime);
    }
    }

    • @supapaw
      @supapaw Год назад +1

      Thank you, that was so helpful!

    • @jamesviant5075
      @jamesviant5075 6 месяцев назад

      You are a life saver my friend!

  • @franzpringle6149
    @franzpringle6149 2 года назад +4

    helped me a lot! had some problems with the rotation, this video explained it well. ty!

    • @DanPos
      @DanPos  2 года назад

      Glad it helped!

  • @omkarpatil2094
    @omkarpatil2094 2 года назад +1

    Thank you so much for the video. I was having issues with character rotation and this solved it for me! much love!

    • @DanPos
      @DanPos  2 года назад

      No worries!

  • @splashgy3038
    @splashgy3038 6 месяцев назад

    Thank you, pretty much on-point tutorial. Works fine for 2022.3.5f!

  • @hawkgamedev
    @hawkgamedev 2 года назад

    Dan you are one of the best Unity youtubers out there keep it up man. I really find your videos super useful. If I ever publish a game you bet you'll be in the credits.

    • @DanPos
      @DanPos  2 года назад +1

      I appreciate that!

  • @tri99er_
    @tri99er_ Год назад

    Thank you very much. I was bashing my head against the wall trying to figure out a way to make good look at mouse position implementation (tried it with ScreenToWorldPoint, and it works, but very bad accuracy).

  • @DevonJenkin
    @DevonJenkin 2 года назад +3

    Amazing tutorial :) This really helped me and it is extremely well made so thank you! I was wondering if there is a way to rotate the character with the input of the left stick and have a seperate object rotate with the right (I am making a tank top down game and wish for the turret to rotate with right stick and the tank to rotate in the direction of which it is moving with the left stick) Thanks again Dan I have used many of your tutorials and you clearly put a lot of time and effort into them you deserve wayyyy more subs

  • @luigitan5602
    @luigitan5602 Месяц назад

    This is really old but, when I assign the player to the [Follow] space of the virtual camera it stops me from manually moving the camera with the arrows. Is there a way to fix it? Could it be because of the unity version?

  • @Candyhead
    @Candyhead Год назад +2

    If anyone wants to add smoothness to the mouse rotation script, here is the code:
    void HandleRotation()
    {
    if (isGamepad)
    {
    // gamepad code
    }
    else
    {
    // Mouse rotation code
    Ray ray = Camera.main.ScreenPointToRay(aim);
    Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
    float rayDist;
    if (groundPlane.Raycast(ray, out rayDist))
    {
    Vector3 point = ray.GetPoint(rayDist);
    SmoothLookAt(point);
    }
    }
    }
    void SmoothLookAt(Vector3 lookPoint)
    {
    Vector3 heightCorrectedPoint = new Vector3(lookPoint.x, transform.position.y, lookPoint.z);
    Quaternion targetRotation = Quaternion.LookRotation(heightCorrectedPoint - transform.position, Vector3.up);
    transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, mouseRotateSmoothing * Time.deltaTime);
    }

  • @fancygraystuff
    @fancygraystuff Год назад

    I learned so much from this man great work

    • @DanPos
      @DanPos  Год назад

      My pleasure!

  • @blackskull
    @blackskull 6 месяцев назад

    Hey great tutorial! After looking through all the code im having an issue where my WASD controls cause my character to move extremely slowly after inputting the mouse aim code. Any ideas as to why?

  • @alienouz
    @alienouz 2 года назад

    Amazing video, I really like it and was just what I was looking for.
    I think you can extend this to make it a top down shooter.
    would love to see this tutorial

  • @miroslaugh
    @miroslaugh 2 года назад +8

    For some reason my control scheme wouldn't switch between gamepad and kbm. I googled it and found a fix. I opened the PlayerControls Input Action and added a required gamepad to the gamepad scheme and a required keyboard/mouse to the KBM scheme

    • @thomasboone39
      @thomasboone39 2 года назад +1

      Thank you, good sir

    • @ZzzzzQQ
      @ZzzzzQQ 2 года назад

      I have the same problem. The input debugger on the player shows that the control scheme is always gamepad. Could you please explain how to add a required keyboard/mouse in details? Thank you so much!

    • @thomasboone39
      @thomasboone39 2 года назад +1

      @@ZzzzzQQ Open the input actions window and go to the left top corner, there you can add them

    • @ZzzzzQQ
      @ZzzzzQQ 2 года назад +1

      @@thomasboone39 Thank you so much! I have fixed it!

  • @ElNerdgasmo
    @ElNerdgasmo 2 года назад

    now i try figure add shoot mechanics, thanks a lot

    • @DanPos
      @DanPos  2 года назад

      No worries!

  • @funkychunkymonkey77
    @funkychunkymonkey77 Месяц назад

    I am getting an error stating my Vector3.right cannot be assigned to -- its read only? But I've went over the code like 10 times now and cant see any issues. Help please

    • @DanPos
      @DanPos  Месяц назад

      Yeah you can't assign to Vector3.right you must be trying to set it to something with an = sign. If you read the error fully it will tell you the exact line you're doing this in.

  • @charliesinho8451
    @charliesinho8451 2 года назад +2

    I am sure at this point that the code is right, but when I add twin stick it wont add the Player Input and even if I manually add it my character wont move, please do you know why this is happening?

    • @j0shplayz885
      @j0shplayz885 2 года назад +2

      Same here did you figure it out?

    • @charliesinho8451
      @charliesinho8451 2 года назад

      @@j0shplayz885 Not yet, I even posted this in the forums with no response either, everything seems to be alright up until the point of the first playtest but not even the gravity works, the character just wont do anything and in the console says that none of the serialized pattametters are being used anywhere

    • @charliesinho8451
      @charliesinho8451 2 года назад

      @@j0shplayz885 Did you figure it out?

    • @DanPos
      @DanPos  2 года назад

      Can you join the discord and post there and share your code?

  • @RafaelBorgesFerreira-b9f
    @RafaelBorgesFerreira-b9f Год назад

    Thanks for the video, help me so much

  • @RiseofMortals
    @RiseofMortals Год назад

    Is there a way to mimic the way a joystick functions with this system on a mouse? Like not displaying the cursor and maybe somehow only factoring mouse direction objectively and not subjectively based on the location on the screen? As in, if I move my mouse left, the character looks left and it doesn't really matter where my cursor actually is on the screen (if it were visible).

  • @NickHazrd
    @NickHazrd 2 года назад

    Have a strange error where the mouse doesn't rotate properly and it doesn't go around 360. it only goes 180 no matter what. any thoughts are appriciated.

  • @DanielTrinidad-v1w
    @DanielTrinidad-v1w 2 месяца назад

    How can i use this with two players, both with gamepads?

  • @ludomancerstudio
    @ludomancerstudio Год назад

    Hey so the problem I'm having with your code is that either the character rotates to slowly or it instantly rotates but only to the 8 directions of the controller, how can I fix this?

    • @DanPos
      @DanPos  Год назад

      I'd suggest rewatching and following along again to see what you missed

  • @HelloMrGreen
    @HelloMrGreen Год назад

    Amazing video thank you! :)

  • @reachness7628
    @reachness7628 2 года назад +1

    I find that using this method for calculating fall velocity has an issue, it starts integrating the gravity accelerations always even when in contact with the ground so when it should fall it actually falls too fast.

  • @KaizerSoze05
    @KaizerSoze05 2 года назад

    Helped a lot. Thanks!

    • @DanPos
      @DanPos  2 года назад

      Glad it helped!

  • @neozoid7009
    @neozoid7009 2 года назад

    AWESOME love you thanks for the awesome tutorial just what I am looking for . By the way how did you Deug the controller scheme in the inspector ?? But rotation for KBM is not working when camera is too lower ? please help

  • @bemblake6390
    @bemblake6390 3 года назад +1

    Hi there. For rotation my mouse works but not my right analog stick on my xbox controller. Luckily the left stick works for movement. I've looked and can't seem to maybe find out what's causing it. Any idea why?

    • @DanPos
      @DanPos  3 года назад

      The two things I would go back and check are - you've mapped it correctly in the input action asset including the action being of the correct type. Following that I would recheck the code looking for typos.
      I would also try printing the value of the MouseLook axis to the console and make sure you are receiving input.

    • @jollzy887
      @jollzy887 3 года назад

      @@DanPos I am experiencing the same thing. I am receiving input, since the right stick "resets" the rotation, at about 200 degress.
      "Is Gamepad" didn't get checked, when I used the controller. I could move the capsule, but not rotate. I checked the box in the Twin Stick script, and it works perfectly. Need to fix why is doesn't do it automatically though.
      Thank you for making these videos, man!

    • @Pointcut
      @Pointcut 3 года назад

      @Andreas Hein The reason this occurs is because the name for a "Gamepad" control scheme is actually "Controller", so the code should say:
      *isGamepad = playerInput.currentControlScheme.Equals("Controller")*

    • @DanPos
      @DanPos  3 года назад

      I'd also check out this video which may help ruclips.net/video/vqtzus9i1so/видео.html

    • @bagabeans
      @bagabeans 2 года назад +2

      @@DanPos I've been trying to solve these two same issues for hours now. The right stick not working/isGamepad not being checked was because I missed the part at 6:45 where you add the the Gamepad to the Gamepad Control Scheme (and the Keyboard and Mouse to KBM) like your extra vid shows.
      I'm still having the issue with the mouse rotation constantly resetting the player rotation. I've drawn a line to the LookAt(point) and discovered something is causing it to snap to very bottom left point of the camera. I've used someone else's rotation code and it does the same, and I've tried deleting and re-adding the cameras in with no joy. I'm out of ideas now, I just can't find anything in your tutorial that's different and nothing in the code that would prompt the rotation to get set to that point every time.

  • @LesusGames
    @LesusGames 2 года назад

    Heads up, and dunno if this a new update or anything, but I lost a lot of time to this. My playerControls reference wasn't returning any values with ReadValue(). Fixed it by adding playerControls.Enable(); in Awake.

    • @DanPos
      @DanPos  2 года назад

      You can see at 10:15 I add this in OnEnable and also Disable them in OnDisable :)

  • @seriouissam
    @seriouissam 2 года назад

    thank you!

  • @isaacashton5772
    @isaacashton5772 9 месяцев назад

    will this work for 2D? i can’t find any top down twin stick shooter games!

  • @heftykat
    @heftykat 2 года назад

    Is there a line of code in this that can be changed to account for isometric movements? I am having trouble adjusting the code

    • @AmplifickDesignsTMxD
      @AmplifickDesignsTMxD 11 месяцев назад

      Hey! My message can be too late but if there's someone else needing this, what you need to do is rotate the whole matrix 45 degrees (I suppose your view is rotated 45 degrees ). To achieve this you must add the following lines
      Matrix4x4 matrix = Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));
      Vector3 skewedInput = matrix.MultiplyPoint3x4(playerDirection);
      Now replace all the playerDirection with skewedInput and this will make the rotation! You can do the same thing for movement!
      (In my case, I only did it for movement and controller rotation, not for mouse rotation)
      Hope this helps!

  • @0u73rh34v3n
    @0u73rh34v3n 2 года назад +1

    Anyone have any success with this tutorial in Unity 2022.1.23f1?
    Getting the error below and OnDeviceChange() doesn't appear in the list of Control Changed Events under TwinStickMovement
    ArgumentException: GetComponent requires that the requested component 'PlayerInput' derives from MonoBehaviour or Component or is an interface.
    UnityEngine.GameObject.GetComponent[T] () (at :0)
    TwinStickMovement.Awake () (at Assets/Scripts/TwinStickMovement.cs:31)

    • @jasonisaderro
      @jasonisaderro Год назад +1

      Yep I have the same issue. have you found a fix yet?

    • @alex-et7tn
      @alex-et7tn Год назад

      Try with controller = GetComponent(); not use “.”, changue for “= “

  • @Dylen
    @Dylen 2 года назад

    18:40 I'm really struggling to make this work on 2D :( any ideas? Changing Vectors to Vector2 is not enough. I dont really understand the rotation logic on 3D so I cant make it 2D. I only need to rotate my character in the Z-Axis

    • @DanPos
      @DanPos  2 года назад +1

      Check out this forum post, has a great answer: forum.unity.com/threads/look-rotation-2d-equivalent.611044/

    • @Dylen
      @Dylen 2 года назад

      @@DanPos Dude thank you so fking much I love you. QUACKS for you and a lot of LOVE

    • @BlueReeceo2
      @BlueReeceo2 2 года назад

      @@Dylen I'm struggling to make sense of this, how did you manage to do this?

  • @rhyswittingslow
    @rhyswittingslow 2 года назад

    Hey dan, great video. Any chance you could give me a tip into offsetting the aim from the camera position. I have the camera offset in cinemachine and then i press in a direction the aim is clearly offset. What should I add to the rotation code?

    • @DanPos
      @DanPos  2 года назад +3

      If I understand you correctly, I suppose you would need to do something to work out the play position in relation to the cursor position. For example you may be able to subtract the play position from the aim position and use that? Not 100% sure if that would work but could be a good starting point

  • @ianpercy1333
    @ianpercy1333 2 года назад

    Going to comment here even though I don't think it will be an easy fix. All my code has been triple-checked but it seems to rotate only on the x-axis and faces directly upwards instead of in the direction specified. It starts out correctly, but eventually (after rotating for a complete 360) faces upwards and only rotates on the X instead of Y.

    • @DanPos
      @DanPos  2 года назад

      Can you join the discord and share your code?

    • @ianpercy1333
      @ianpercy1333 2 года назад +1

      @@DanPos No worries!! After two full weeks (despite only asking for help yesterday) I figured it out. I'm instantiating the player, and instead of Quaternion.identity for its rotation, I was using the spawn location's rotation (purposefully, I wanted the characters to face a certain direction on spawn), but it was for some reason causing the whole object to rotate differently since the objects were tilted 90 degrees. I have to clue why but changing this fixed it. Of course it would be an issue from another script I made and not your tutorial haha
      Thanks anyways for the response, I appreciate it!

  • @ivancardealnunes
    @ivancardealnunes 3 года назад

    Thanks!

    • @DanPos
      @DanPos  3 года назад

      No problem!

  • @Vasquez2701
    @Vasquez2701 3 года назад

    Hello! I bought your Patreon just for this tutorial and I can´t seem to find it lol. Will you upload it there?

    • @DanPos
      @DanPos  3 года назад +1

      Hi Andres first, thanks for subscribing! Secondly I will take a look as it should be up, I'll message you on Patreon once it's sorted!

    • @Vasquez2701
      @Vasquez2701 3 года назад

      @@DanPos Thank you mate!

  • @scoobaas7285
    @scoobaas7285 2 года назад

    hey, how can i modify this script to add another player that uses another gamepad

    • @DanPos
      @DanPos  2 года назад

      Have you checked out my local coop series? You could implement that with this

    • @scoobaas7285
      @scoobaas7285 2 года назад +1

      @@DanPos no, but i will. thank you so much!

  • @iR3dyOfficial
    @iR3dyOfficial Год назад

    Crosshairs?

  • @jonnchambers7606
    @jonnchambers7606 Год назад

    Where is the playerinput script?

  • @generalroffmayer3319
    @generalroffmayer3319 2 года назад

    Error script twindstick..v " CS1022 " type or namespace definition , or end -of-file expected

    • @DanPos
      @DanPos  2 года назад

      Sounds like you may have lost a curly brace at the end of the script, that error also says twinDscript - may be a typo somewhere in the file too as you've put the letter d in there

  • @Foxxyz_TV
    @Foxxyz_TV 2 года назад

    hi, this programming would work in 2D too, wouldn't it?

    • @DanPos
      @DanPos  2 года назад

      I don't see why not! You will probably need to just change some of the vector stuff from Vector3 to Vector2

    • @Foxxyz_TV
      @Foxxyz_TV 2 года назад

      @@DanPos just one more thing, could i use rotation separate from movement???

  • @دعاءخالد-ش5م
    @دعاءخالد-ش5م 4 месяца назад

    I need this in 3d how ?

    • @DanPos
      @DanPos  4 месяца назад

      This IS 3d?

  • @loganthomas5959
    @loganthomas5959 3 года назад

    any tips on doing this with 2d sprites?

    • @DanPos
      @DanPos  3 года назад

      I don't work in 2D so I'm unsure sorry!

  • @williamburke6562
    @williamburke6562 2 года назад

    My character just looks at the bottom left of the screen no matter what.

    • @DanPos
      @DanPos  2 года назад

      I'd recommend going back through and checking your code against mine - for example i just helped a patreon debug their code and they had put just one line wrong and it messed everything up for them.

    • @BrendonCrato
      @BrendonCrato 2 года назад

      Did you figure it out?

  • @aksishwu
    @aksishwu 2 года назад

    Спасибо очень помогло

    • @DanPos
      @DanPos  2 года назад +1

      Не за что

  • @DanPos
    @DanPos  3 года назад

    ⏰ UNITY BLACK FRIDAY SALE ENDS DECEMBER 4th!
    ⚡ LIGHTNING DEALS SEE 90% OFF SELECT PRODUCTS
    Take 50% off the best selling assets on the Unity asset store in their Black Friday sale, now on!
    prf.hn/click/camref:1101lkANY/creativeref:1011l65259

  • @КонстантинМельников-г4т

    lol, such a good code:
    ```
    isGamepad = blabla.Equals("Gamepad") ? true : false;
    ```
    Why? Really? `Equals` method return boolean already. Why the hell you wanna use ternary operator to return true on true & false on false :D ?

    • @DanPos
      @DanPos  2 года назад +1

      Yes this was a stupid oversight - good spot!

    • @Dylen
      @Dylen 2 года назад +2

      Very polite way of pointing out a mistake

  • @DaniBARROPALACIOS
    @DaniBARROPALACIOS 2 года назад +1

    Thanks! I just only have one problem, for some reason this line
    Ray ray = Camera.main.ScreenPointToRay(aim);
    gives me an error, I need help, thanks

    • @Eqqs
      @Eqqs Год назад

      I have the same error, please let me know if you fixed it.

    • @VivianKitsune
      @VivianKitsune Год назад +2

      @@Eqqs You need to set your camera tag to MainCamera

    • @Eqqs
      @Eqqs Год назад

      @@VivianKitsune I have fixed it a while ago but yes, I confirm that this was the issue! Thanks.