2D Player Rotation in the Direction of Input (Unity Tutorial | 2D Top Down Shooter)

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ธ.ค. 2024

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

  • @pr4shantishere
    @pr4shantishere 2 ปีที่แล้ว +7

    Absolutely in love with your tutorials.

    • @KetraGames
      @KetraGames  2 ปีที่แล้ว +3

      Thank you 😊🥰

  • @redtapecollection3363
    @redtapecollection3363 2 ปีที่แล้ว +3

    Can't wait for the shooting part! Hope it comes out soon!

    • @KetraGames
      @KetraGames  2 ปีที่แล้ว +1

      It's not too far away 😊

  • @markmccorkle25
    @markmccorkle25 2 ปีที่แล้ว +7

    Very concise and well explained. Thank you!
    One challenge I've had with this top down game mechanic like this is how to deal with aiming the weapon opposite the direction that I'm moving. This happens both with the "aim at mouse cursor + WASD movement" and the dual stick xbox "left stick moves, right stick aims" control schemes. Any chance you have a video coming out to solve that challenge?

    • @KetraGames
      @KetraGames  2 ปีที่แล้ว +6

      Hi Mark, thanks for your support 😊
      The plan for this series was to have the player shoot in the direction it's moving, but thinking about it, it's probably also worth including 'aim at mouse cursor' as an alternative control mechanism. It'll be while before we get to it though so in the meantime this code extract may help
      private void RotateInDirectionOfCursor()
      {
      Vector3 mouseWorldPosition = _camera.ScreenToWorldPoint(_mousePosition);
      Vector2 targetDirection = mouseWorldPosition - transform.position;
      targetDirection.Normalize();
      Quaternion targetRotation = Quaternion.LookRotation(transform.forward, targetDirection);
      var rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, _rotationSpeed * Time.deltaTime);
      _rigidbody.MoveRotation(rotation);
      }
      To aim at the cursor you need to convert the mouse position to world space. Then you can work out the direction the cursor is from the player. Finally you can rotate towards this target direction in the same way as this video.
      Hope that helps 😊

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

      @@KetraGames Thank you so much for including this code snippet, I'm trying to include right stick controller rotation using the new input system and I'm kinda having a hard time figuring out how to use that when controller input is detected.. Is there any chance I could get some pointers?

    • @ddogg14
      @ddogg14 10 หลายเดือนก่อน

      @@KetraGames Thank you, this code extract helped me out immensely!

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

    Hi, so I have my main camera following the player, is there a way so the camera doesnt rotate when the sprite rotates?

    • @AlfurnoGaming
      @AlfurnoGaming 9 หลายเดือนก่อน

      Don't put your camera inside the player. Create a script that moves the player to the camera.

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

    first! u are so good

    • @KetraGames
      @KetraGames  2 ปีที่แล้ว +1

      👍Thank you 😊

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

    Great video!

  • @tranquil3727
    @tranquil3727 2 ปีที่แล้ว +1

    What happened to the other videos in this playlist? I wanted to finish the tutorial :(

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

      Hi, the other videos haven't been released yet. We're making them as fast as we can 😊

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

      @@KetraGames oh, my mistake 😅

  • @itsmelvintime
    @itsmelvintime 2 ปีที่แล้ว +1

    Whenever I press the right arrow key, it faces downward and not right. Same thing for the left arrow key except that it turns upward.
    And whenever I press the up arrow key, it faces left.
    Is there any chance that this code doesn't work in version 2021.3.16f1?

    • @KetraGames
      @KetraGames  2 ปีที่แล้ว +1

      Hi, is your character facing upwards towards the top of the screen before you press play? The parent Player object should have no rotation and the child graphics object should be rotated by 90 degrees

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

      @@KetraGames I have the same problem and the character is facing right

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

      @@KetraGamesNevermind i fixed it, i just changed the graphics z rotation to 90

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

    Thanks for your videos, they are awesome!

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

      Thanks for this comment 😊

  • @thedipcifican8302
    @thedipcifican8302 8 หลายเดือนก่อน

    I've copied the code from the website you guys provided but I cannot get the model rotating even after triple checking the code. Is there anything else that could stop it from rotating?

    • @KetraGames
      @KetraGames  6 หลายเดือนก่อน

      Hi, some people have reported issues using the MoveRotation method. Try changing it to SetRotation. Hope that helps 😊

    • @bethedust
      @bethedust 4 หลายเดือนก่อน

      @@KetraGames It helped! thanks

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

    One question here: when I go to visual studio and select the codes then hit "quick actions and..." the program says that "no quick actions avaible here". Any suggestions what did I do wrong? Anyway, great tutorial, thanks a lot for it! I would like to see a movement tutorial, where you can learn a simple rpg game movement with animation, that would be cool too!

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

      Hi, not sure why you aren't getting the quick actions. Have you selected the code you want to apply the action to? If you can't get it to work then you can do it by hand. It's just not as quick 😊

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

      @@KetraGames Thanks 😊

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

    Hello Ketra Games! I am at an impasse about the movement of the sprite. My game's sprite has four different sprites that have four unique walking animations for each direction, being up, down, left and right. Meanwhile, yours has the one with animation, which rotates in whatever direction the player will go in. Is there anyway I can set up this movement while keeping the four animations? I wanted to see before actually doing all the work, just in case if it doesn't. Thank you.

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

      Hi, I think you'll probably want to skip this rotation step and instead swap to the relevant animation depending on the direction of input. Hope that helps 😊

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

    how do i make it rotate towards the mosue?

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

      Hi, we'll cover this at a later point in the series. If you can't wait then the full project, including rotate toward mouse, is available to our 'All Access' Patrons here - www.patreon.com/posts/full-project-for-75231350 😊

  • @CoolDude-m2c
    @CoolDude-m2c 2 หลายเดือนก่อน

    How do you do this if you didnt do the smooth input.

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

    Can you help me.. Cause i dont get the quick action refactoring? I did the same as you

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

      Hi, if you can't get the quick refactoring to work, then you can just copy the changes manually. It's not as quick but as long as the end result is the same then it will work. Hope that helps 😊

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

      @@KetraGames I got it and I did it thank you 🔥.. Btw I didn't get the 12 2D free background assets about the battle background .. Is it over or how?

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

      @@PungPortal Yes, unfortunately those background are no longer available. We created a similar one here - drive.google.com/file/d/1ACDZNthP0vIpFLBOLvJbI6uIVhM_IEGo/view?usp=sharing. Or you can use any other background image you like 😊

  • @jade-nn9bt
    @jade-nn9bt ปีที่แล้ว

    I am trying to follow along but I keep getting the same error that quaternion dose not contain a definition for RotateTowards

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

      Hi, have you tried copying the code from here? dotnetfiddle.net/6DWkDw

  • @nmd2899
    @nmd2899 7 หลายเดือนก่อน

    Hey , Everything worked fine but rotation is weird , like it rotates suddenly and looks so much weird , any idea how to fix this ?

    • @KetraGames
      @KetraGames  6 หลายเดือนก่อน +1

      Hi, some people have reported issues using the MoveRotation method. Try changing it to SetRotation. Hope that helps 😊

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

      Hi @nmd2899 The problem is that the player input and the player image are not in the same location.

  • @robertcurley5120
    @robertcurley5120 9 หลายเดือนก่อน

    great video but my character is 90 degrees off; if i move left then it looks up and if i move up it looks right, can you help
    😆

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

    I have a question, is it possible instead of rotating to the direction you are looking, to rotate to the mouse position? I have tried to do it myself, but it didn't work :P.

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

      Hi, we'll cover this at a later point in the series. If you can't wait then the full project, including rotate toward mouse, is available to our 'All Access' Patrons here - www.patreon.com/posts/full-project-for-75231350 😊

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

      @@KetraGames Okay, thanks for your explanation :D

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

    Assets\playerscript\PlayerMovement.cs(4,19): error CS0234: The type or namespace name 'InputSystem' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?)
    Assets\playerscript\PlayerMovement.cs(52,25): error CS0246: The type or namespace name 'InputValue' could not be found (are you missing a using directive or an assembly reference?)
    I HAVE THIS ERRORS, PLS HELP ME TY

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

      Hi, have you installed the Input System package from the package manager?

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

    It says 2 of the videos are currently hidden. Are the hidden videos intended for patreons?

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

      Hi, no they were deleted videos that were still in the playlist somehow. We've now removed them. Thanks for letting us know 😊

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

    very good , but i still dont understand why the child , and how it doesnt work if we dont have the child object to the player

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

    its not working, i follow all the instruction, even i coppy ur code still not working, i am already install inputSystem too

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

    almost what i want :( but only rotates rigidbody not gameobjects

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

    the exract method is not working!

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

      Hi, if that option isn't available you can just manually change the script to match. It will take a bit longer but it should still work 😊

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

      @@KetraGames i cant change the "private void fixedupdate" into setplayervelocity.. how can i fix it

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

    The Final script have errors

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

      Hi, what errors are you seeing? Can you compare yours with the final script here to see if there are any differences? - dotnetfiddle.net/6DWkDw

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

    This is bunk...The code doesn't work. I have spent days now trying to figure out why I cannot even get the player to move...thought this would expand upon it. But no. Your code doesn't work.
    ...

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

      Hi, sorry to hear that it's not working for you.
      Here are some suggestions for things to check
      1. Are there any errors in the console window?
      2. Have you installed the new input system from the Package Manager
      3. Have you added the Player Input component to the Player and assigned the actions?
      Hope that helps 😊

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

    🎯 Key Takeaways for quick navigation:
    - The video introduces how to make a game object rotate to face the direction it's moving in.
    - The video uses a 2D top-down shooter game as an example.
    - The video provides two ways to rotate a sprite: using a photo editing application or using Unity.
    - The video explains how to extract the movement logic into a method called "Set Player Velocity".
    - This method is used to set the velocity of the player object.
    - Extracting the movement logic into a method makes the code more organized and easier to understand.
    - The video explains how to rotate the player object in the direction of input using the "Look Rotation" method.
    - The "Look Rotation" method takes two parameters: the desired forward direction and the desired up direction.
    - The video explains that the forward direction is not relevant for 2D games, so the current value is used.
    - The video explains that the up direction is set to the direction the player is moving.
    Made with HARPA AI

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

    it does not rotate in the direction of input

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

      Hi, does it rotate at all?

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

    I can't believe unity doesn't have a simple character controller you can drop into a game and use right away. That is very bad for an engine this popular.

  • @thetwigler
    @thetwigler 3 หลายเดือนก่อน

    trying to do this on mac is infuriating

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

    ooga booga

  • @coolorphans
    @coolorphans 2 ปีที่แล้ว +1

    Thanks baby girl