Тёмный

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

Ketra Games
Подписаться 23 тыс.
Просмотров 21 тыс.
50% 1

In this Unity 2D tutorial we're going to look at how to make a game object rotate, to face the direction it’s moving in.
This is the third video in our series showing how to make a full top down 2D shooter game in Unity.
The playlist for this series can be found here
► • Unity 2D Top Down Shoo...
The project files are available to our patrons here:
► / 74343725
Want to follow along? This project is a good starting point
► / 73974815
The final script can be found here
► dotnetfiddle.net/6DWkDw
Help support our work:
► Patreon: / ketragames
Follow us:
► Ketra Games: www.ketra-games.com
► Patreon: / ketragames
► Twitter: / ketragames
► Facebook: / ketragames
Introduction - 0:00
Rotating the Player Sprite - 0:30
Rotating the Player in the Direction of Input - 2:06
Summary - 5:56
#KetraGames #LearnUnity #UnityTutorials #UnityTips

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

 

25 июн 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 69   
@pr4shantishere
@pr4shantishere Год назад
Absolutely in love with your tutorials.
@KetraGames
@KetraGames Год назад
Thank you 😊🥰
@redtapecollection3363
@redtapecollection3363 Год назад
Can't wait for the shooting part! Hope it comes out soon!
@KetraGames
@KetraGames Год назад
It's not too far away 😊
@nogrilodoguede7515
@nogrilodoguede7515 Год назад
Thanks for your videos, they are awesome!
@KetraGames
@KetraGames Год назад
Thanks for this comment 😊
@DmanGames
@DmanGames Год назад
Great video!
@KetraGames
@KetraGames Год назад
👍😊
@markmccorkle25
@markmccorkle25 Год назад
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 Год назад
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 9 месяцев назад
@@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 4 месяца назад
@@KetraGames Thank you, this code extract helped me out immensely!
@zhoking0075
@zhoking0075 Год назад
first! u are so good
@KetraGames
@KetraGames Год назад
👍Thank you 😊
@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 😊
@Basilisk24357
@Basilisk24357 10 месяцев назад
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 4 месяца назад
Don't put your camera inside the player. Create a script that moves the player to the camera.
@jeremylock7618
@jeremylock7618 Год назад
It says 2 of the videos are currently hidden. Are the hidden videos intended for patreons?
@KetraGames
@KetraGames Год назад
Hi, no they were deleted videos that were still in the playlist somehow. We've now removed them. Thanks for letting us know 😊
@tranquil3727
@tranquil3727 Год назад
What happened to the other videos in this playlist? I wanted to finish the tutorial :(
@KetraGames
@KetraGames Год назад
Hi, the other videos haven't been released yet. We're making them as fast as we can 😊
@tranquil3727
@tranquil3727 Год назад
@@KetraGames oh, my mistake 😅
@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 Год назад
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 😊
@robertcurley5120
@robertcurley5120 3 месяца назад
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 😆
@thedipcifican8302
@thedipcifican8302 2 месяца назад
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 Месяц назад
Hi, some people have reported issues using the MoveRotation method. Try changing it to SetRotation. Hope that helps 😊
@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
@itsmelvintime
@itsmelvintime Год назад
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 Год назад
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
@nmd2899
@nmd2899 2 месяца назад
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 Месяц назад
Hi, some people have reported issues using the MoveRotation method. Try changing it to SetRotation. Hope that helps 😊
@starnight___
@starnight___ 11 месяцев назад
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
@tankycatcool63
@tankycatcool63 Год назад
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 😊
@PungPortal
@PungPortal 10 месяцев назад
Can you help me.. Cause i dont get the quick action refactoring? I did the same as you
@KetraGames
@KetraGames 10 месяцев назад
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 10 месяцев назад
@@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 8 месяцев назад
@@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 7 месяцев назад
I am trying to follow along but I keep getting the same error that quaternion dose not contain a definition for RotateTowards
@KetraGames
@KetraGames 7 месяцев назад
Hi, have you tried copying the code from here? dotnetfiddle.net/6DWkDw
@pfp4ever
@pfp4ever Год назад
almost what i want :( but only rotates rigidbody not gameobjects
@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
@poilkjlkj
@poilkjlkj Год назад
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.
@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
@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
@tankycatcool63
@tankycatcool63 Год назад
it does not rotate in the direction of input
@KetraGames
@KetraGames Год назад
Hi, does it rotate at all?
@tankycatcool63
@tankycatcool63 Год назад
ooga booga
@legendaryfasteddy
@legendaryfasteddy Год назад
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 Год назад
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 7 месяцев назад
🎯 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
@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?
@coolorphans
@coolorphans Год назад
Thanks baby girl
@KetraGames
@KetraGames Год назад
👍😊
Далее
MEGA BOXES ARE BACK!!!
08:53
Просмотров 32 млн
Вечный ДВИГАТЕЛЬ!⚙️ #shorts
00:27
Просмотров 956 тыс.
How to Rotate GameObjects in Unity 2d
7:48
Просмотров 3,2 тыс.
Escaping Unity Animator HELL
18:18
Просмотров 496 тыс.
Understand Unity Rotation in 5 minutes
5:23
Просмотров 13 тыс.
How to Rotate in Unity
16:10
Просмотров 21 тыс.
How to Aim a weapon at Mouse in Unity 2D
12:40
Просмотров 35 тыс.