How to Create a Top Down Movement Character Controller in Unity | Scripting Tutorial

  Рет қаралды 74,249

Broken Knights Games

Broken Knights Games

4 жыл бұрын

In this video I demonstrate how to create a top-down character controller movement script in Unity which take into account the rotation offset of the main camera. This video also demonstrates how to make a game object rotate towards the mouse cursor on the screen, as is commonly seen in top down shooters. Learn some basic vector and rotation operations along with a simple and practical physics raycast example in Unity.
Technology Used
Unity 3D Engine 2019.3
Git
Visual Studio 2019
.Net Scripting in C#
Everything shown in the video can be downloaded from my Github repository so you can follow along, or reference my approach to creating a top down character controller component.
Github Repository Link: github.com/Srfigie/Unity-3d-T...
New Input System Tutorial: • Local Co-op in Unity u...
Time Codes:
00:54 Sample Scene Overview
02:05 Input Handler Script Overview
03:02 Character Movement
06:00 Move Relative to camera rotation
10:38 Rotate Towards Movement Vector
15:03 Follow Mouse Rotation
Broken Knights Games:
Website: brokenknights.com
Twitter: / broken_knights
Instagram: / brokenknightsgames
► Music Credit: LAKEY INSPIRED
Track Name: "Better Days"
Music By: LAKEY INSPIRED @ / lakeyinspired
Original upload HERE - www.youtube.com/watch?v=RXLzv...
Official "LAKEY INSPIRED" KZfaq Channel HERE - kzfaq.info/love/Omy...
License for commercial use: Creative Commons Attribution 3.0 Unported "Share Alike" (CC BY-SA 3.0) License.
Full License HERE - creativecommons.org/licenses/...
Music promoted by NCM goo.gl/fh3rEJ

Пікірлер: 74
@tientam779
@tientam779 3 жыл бұрын
This video is great. It really helped me with creating an isometric character controller. You not only covered the movements but also smoothing out things like rotation and point and click movement. Hope to see more great tutorials from you in the future!
@RameoMTL
@RameoMTL 4 жыл бұрын
Awesome video! I'm super new to programming and Unity so this gives me great insight! One thing to not overlook is the value of talking to yourself or somebody when problem solving. It really helps walk you through the process thoroughly.
@unalouw2023
@unalouw2023 4 жыл бұрын
Really great tutorial, thanks! You get to the point, but you still explain what everything means/does. Helped me a lot :)
@iamthebubb
@iamthebubb Жыл бұрын
This solution is perfect thank you, no one else has this method for smooth movement isometric and smooth turning, I looked everywhere!
@colorbreakers2383
@colorbreakers2383 4 жыл бұрын
Amazing tutorial! Movement is one of the most important things in every game and a lot of people give it for granted, but you need to fine tune it until it feels great. Thanks for this video!
@brokenknightsgames7966
@brokenknightsgames7966 4 жыл бұрын
Agreed! The standard assets pack has great character controllers you can use but it's important to understand how they work vector math can be hard sometimes
@luansantos8501
@luansantos8501 3 жыл бұрын
Thank you very much man, you helped me a lot, I needed this for the cell phone's joystick, I made some modifications and it worked very well, thank you again.
@kuzetti
@kuzetti 3 жыл бұрын
You make great videos! Easy to follow along and learn.
@orchard800
@orchard800 3 жыл бұрын
Keep up the awesome content. Animating the character next would be awesome!
@arc5015
@arc5015 2 жыл бұрын
Yoo thank you for the mouse look rotation bit, big help. Good tut :).
@rredenvous
@rredenvous 2 жыл бұрын
amazing tut and it works flawlessly THANK YOU!!!
@toast_hawaii
@toast_hawaii 3 жыл бұрын
For everyone having troubles controlling there movement, for me it helped to use this code in the Update funtion: float moveSpeed = 10; //Define the speed at which the object moves. float horizontalInput = Input.GetAxis("Horizontal"); //Get the value of the Horizontal input axis. float verticalInput = Input.GetAxis("Vertical"); //Get the value of the Vertical input axis. transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * moveSpeed * Time.deltaTime); //Move the object to XYZ coordinates defined as horizontalInput, 0, and verticalInput respectively.
@damianoschirinzi4317
@damianoschirinzi4317 3 жыл бұрын
Great tutorial! Really appreciate it dude :)
@LaQuintaPataDebate
@LaQuintaPataDebate 3 жыл бұрын
Great tuto! thanks. Waiting for a full top down tutorial!!!
@eddyrc7
@eddyrc7 2 жыл бұрын
Your first 30 seconds for me were like the story of my life... insta sub insta loving your video.
@beekall1781
@beekall1781 2 жыл бұрын
right at the point! Fantastic! thx
@l.g5390
@l.g5390 Жыл бұрын
Best tutorial ever helped me a lot ❤
@redeye1k
@redeye1k 2 жыл бұрын
Thanks Bro Awesome Video Keep It Up!
@eijstech
@eijstech 3 жыл бұрын
BRO WHERE HAVE U BEEN ALL MY LIFE!?
@axbox8543
@axbox8543 2 жыл бұрын
Thanks a ton! This really helped
@blackblocky3866
@blackblocky3866 2 жыл бұрын
Helped me a lot!
@thet00nl1nk3
@thet00nl1nk3 2 жыл бұрын
Fantastic tutorial, clear, concise and you even showed a nice toggle for the controls! One thing I would add though (and kind of important) is that on line 62, the targetVector doesn't get normalized, so you'll move quicker in diagonal directions if you push horizontal and vertical keys simultaneously. So I would just add in between line 62 and 63: targetVector = Vector3.Normalize(targetVector); Referencing time stamp around 20:00.
@brokenknightsgames7966
@brokenknightsgames7966 2 жыл бұрын
This is a great suggestion!
@nerdin1805
@nerdin1805 2 жыл бұрын
Thanks for help)
@siva6008
@siva6008 2 жыл бұрын
Yo man you are brilliant 😍
@alexis17642
@alexis17642 Жыл бұрын
Merci pour le tutoriel efficace!
@931112
@931112 3 жыл бұрын
great content!
@pixys6856
@pixys6856 2 жыл бұрын
Thanks a lot man!
@muriilouwu
@muriilouwu 2 ай бұрын
thanks for the video
@flokensai
@flokensai 2 жыл бұрын
Thank You !
@deezaath8085
@deezaath8085 2 жыл бұрын
If anyone want player to rotate smoothly when aiming ,instead of using transform.LookAt(target) try using quaternion.slerp : Vector3 targetVector = new(hit.point.x, transform.position.y, hit.point.z); Quaternion targetRotation = Quaternion.LookRotation(targetVector - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
@bawzoo6600
@bawzoo6600 Жыл бұрын
You have solved a problem I have spent 4 hours trying to figure out. God bless your soul.
@bambino3624
@bambino3624 Жыл бұрын
Ferris Euler's big day off.
@vetirs
@vetirs 4 жыл бұрын
Great tutorial man! i was wondering if its possible to move the player toward to mouse? as well with moving with the wasd keys?
@denniss.3907
@denniss.3907 Жыл бұрын
great tutorial. helped me a lot! is it possible to create a "middle way", that the charakter looks in the right (Z axis) direction but only between +/- 45°?
@Mothmanexe
@Mothmanexe 10 ай бұрын
Epic tutorial!! A small question though, what would I have to change to be able to swap the WASD controls for other keys?
@zidlik6461
@zidlik6461 3 жыл бұрын
Hello, is there a way how to set up that the forward direction will be always where the mouse points? (for example when the mouse is in the left top corner and you hit "w", the character starts moving to the top left corner). Or instead of mouse second (right) joystick on gamepad?
@SuperEckoO
@SuperEckoO 4 жыл бұрын
Hi, thank you for the wonderful tutorial. I would just like to add something about the diagonal movement, now it seems to be too fast. I've looked around a bit and if you add InputVector = new Vector2(h, v).normalized; it seems to fix it.
@brokenknightsgames7966
@brokenknightsgames7966 4 жыл бұрын
Good catch! Thanks for sharing this
@Bobiski
@Bobiski 3 жыл бұрын
Really thank you... This is helped me lot. Can I ask you question? When I put my mouse to character it begins to shaking... How can I fix it?
@Philous
@Philous 3 жыл бұрын
Hey Thnaks a lot :) Just a question is it possible to make possible that where your mouse is, this is the forward? Thx :)
@winstonsmith6143
@winstonsmith6143 3 жыл бұрын
Great tutorial! Can you show how to swap out the capsule for a humanoid character and add animations?
@rayeugene8139
@rayeugene8139 3 жыл бұрын
I realize it is kind of randomly asking but do anybody know a good website to stream new series online ?
@emmittdariel4613
@emmittdariel4613 3 жыл бұрын
@Ray Eugene i watch on Flixzone. You can find it by googling =)
@damaridamien4999
@damaridamien4999 3 жыл бұрын
@Emmitt Dariel Definitely, I have been using Flixzone for years myself :)
@rayeugene8139
@rayeugene8139 3 жыл бұрын
@Emmitt Dariel Thank you, I went there and it seems like a nice service :D Appreciate it!
@emmittdariel4613
@emmittdariel4613 3 жыл бұрын
@Ray Eugene you are welcome =)
@Irishpierre1
@Irishpierre1 3 жыл бұрын
This video was absolutely perfect! Only thing I need to do now is to have my camera follow my player if he moves a certain distance away! I did have a question though....Would it be better (technically) to have our movement in FixedUpdate() instead of calling it in Update()?
@reqq1170
@reqq1170 3 жыл бұрын
yes
@yukoval
@yukoval 2 жыл бұрын
can i just change the commands to keyboard keybinds? or how does that work? is it different?
@keeganbrakhage7835
@keeganbrakhage7835 2 жыл бұрын
sorry im inexperienced, I'm just wondering why do we make an input handler? would it not be more simple to get the input in the same script we move in so we don't have to do all this converting? my best guess is that we have one spot that we can edit or stop all input from easily if we need to, is that correct?
@ntkn
@ntkn 3 жыл бұрын
The package doesnt exist for me even when i hit show preview packages...
@Suntika
@Suntika 3 жыл бұрын
Quaternion?! I gave up all hope in learning c#... Prior to today, if you would have asked me what Quaternions were, I would have said it was a race in Mass Effect. Jokes aside, why is my X movement acceleration slower than my Y movement acceleration? in other words, A and D movement accelerates slower than W and S? any help would be much appreciated.
@machineability
@machineability 4 жыл бұрын
Cool how difficult is it to have speed affected by the terrain slope?
@brokenknightsgames7966
@brokenknightsgames7966 4 жыл бұрын
This can be done using the built in physics engine, where instead of moving the character by sliding the transform, you would apply a force vector to the character's attached rigidbody component and let the physics engine do the work
@iviankasabov8155
@iviankasabov8155 3 жыл бұрын
How would I go on and make the camera follow the player instead of it being static? When I attach it to the player the screen spins uncontrollably... Thanks in advance and great tutorial!
@humblebee3112
@humblebee3112 3 жыл бұрын
You can simply add a script to the camera that sets its position to be that of the players, or you can use cinemachine which does that plus a lot more for you, you would just have to learn to use cinemachine as with any tool. Hope that helps :D also cinemachine is free and awesome so I recommend!
@tournedoproductions5132
@tournedoproductions5132 3 жыл бұрын
How would i make this so when i press for instance W to move forward it actually moves towards the mouse and not in a static FORWARD?
@brokenknightsgames7966
@brokenknightsgames7966 3 жыл бұрын
Check out the RotateTowardsMouseVector method in my github repo. Basically screen to word raycast from the mouse arrow to get a point in world space and then rotate your character to face that point as the character is moves forward. github.com/Srfigie/Unity-3d-TopDownMovement/blob/Minimap/Assets/Scripts/TopDownCharacterMover.cs
@L1ghtOn3
@L1ghtOn3 2 жыл бұрын
Need help with: if(player is moving in world space z or -z direction){do other code here to move UI Icon direction of x direction as the player is being pushed in the x direction always and UI Icon must show this when player is facing in either z or -z world space ditection movement the icon either points right or left depending on the player z movement} The code inside the brackets is fine but the if( player is moving in z or -z world space) I can not figure out 3 weeks later. Alll the best from 🏴󠁧󠁢󠁳󠁣󠁴󠁿👨‍💻 👍
@simonthedigger99
@simonthedigger99 3 жыл бұрын
how can i make it so if i press "w" i go forward in the direction im looking at
@GhosttheassassinSJ
@GhosttheassassinSJ 3 жыл бұрын
hey have you figured this out? I;m stuck on how did he press w to move the character forward as well
@TheXdestruidor
@TheXdestruidor 3 жыл бұрын
Why you constantly uses the "var" variable? Is for to performance of the game?
@brokenknightsgames7966
@brokenknightsgames7966 3 жыл бұрын
var is a keyword in c# that let's you declare a variable without having to specify the type because it can be inferred. For example "var playervariable = new Player();" VS " Player playervariable = new Player(); "
@captainawesome2226
@captainawesome2226 7 ай бұрын
I know this is an old clip but I'm pretty sure using transform.position = (whatever) to move an object instead of setting the rigidbody force or velocity will completely break colliders in your cardinal directions. Wouldn't recommend it.
@TheOnlyOwnedbyCow
@TheOnlyOwnedbyCow 2 жыл бұрын
This causes movement along the x and z values. It is easy enough to fix it to move along the x and y value, but when you try to turn your character, it will not turn properly anymore. How can I change this line to use this value instead? transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotateSpeed); var targetVector = new Vector3(playerInput.InputVector.x, playerInput.InputVector.y, 0); I only changed the line of code to affect the y value instead of z. Using it like this will actually cause the rotation on both the x and y values and not z. This matters because when you put unity in 2d mode, it aligns your screen to the x and y values, not to the x and z values. If I were to continue in unity 2d mode with this coding, absolutely every sprite would need to be rotated so that the camera could view it.
@tacovlogs3145
@tacovlogs3145 3 жыл бұрын
I did the exactly same thing even copied the script from the download, but i have like a bug that when i touch with my cursor a 3d object like the floor of my scene, my character like starts to spin and go crazy, and when my cursor isnt touching a 3d object the rotation doesnt work
@dibaterman
@dibaterman 3 жыл бұрын
This is great, but I'm still absolutely lost on how to revolve the camera around the player while using this. I've found out how to use the mouse to revolve the camera around an object but if I throw this on top of it the results are... unity crashing to say the least. lol
@dibaterman
@dibaterman 3 жыл бұрын
Or rather, how to orbit the camera around the target player. I'm still messing with it though but w/e.
@wurlurd
@wurlurd 3 жыл бұрын
Cannot find 'InputHandler' on Visual Studio. I tried many things but didnt worked. All resources says should use InputActions, Should I ?
@SuujiNoKami
@SuujiNoKami 3 жыл бұрын
He is creating that with the first script. Nothing fancy you can simply use Input.GetAxis() too. The way he does is more logical. I suggest you to learn about classes in unity C# and dividing your script to pieces.
@callummcculley2768
@callummcculley2768 3 жыл бұрын
When the capsule is SuS
@maisonfinale
@maisonfinale 2 жыл бұрын
amogus
@getmotivated5656
@getmotivated5656 Жыл бұрын
sus
Tutorial - How to make a mini-map in Unity
18:14
Broken Knights Games
Рет қаралды 6 М.
THIRD PERSON MOVEMENT in Unity
21:05
Brackeys
Рет қаралды 1,4 МЛН
One moment can change your life ✨🔄
00:32
A4
Рет қаралды 8 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Happy 4th of July 😂
00:12
Pink Shirt Girl
Рет қаралды 61 МЛН
Incredible magic 🤯✨
00:53
America's Got Talent
Рет қаралды 75 МЛН
Data Engineer's Lunch 113: GenAI Under the Hood: How it Works
1:04:16
I Wish I Had Known This Before I Started Unity Game Development...
11:11
Top 10 Game Development Tools For 2024
16:06
Thomas Brush
Рет қаралды 112 М.
Isometric Character Controller in Unity
12:31
Tarodev
Рет қаралды 79 М.
How I learned Unity without following tutorials (Developing 1)
18:11
Game Maker's Toolkit
Рет қаралды 2 МЛН
TOP DOWN MOVEMENT in Unity!
22:30
Brackeys
Рет қаралды 1,2 МЛН
Minecraft but I Open an Airport!
26:09
xNestorio
Рет қаралды 750 М.
I Tried Creating a Game Using Real-World Geographic Data
31:37
Sebastian Lague
Рет қаралды 6 МЛН