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

preview_player
Показать описание
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.

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:

► Music Credit: LAKEY INSPIRED
Track Name: "Better Days"
License for commercial use: Creative Commons Attribution 3.0 Unported "Share Alike" (CC BY-SA 3.0) License.
Рекомендации по теме
Комментарии
Автор

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 = - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

deezaath
Автор

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 =
Referencing time stamp around 20:00.

thetnlnk
Автор

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.

toast_hawaii
Автор

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()?

Irishpierre
Автор

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.

SuperEckoO
Автор

Great tutorial! Can you show how to swap out the capsule for a humanoid character and add animations?

winstonsmith
Автор

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.

RameoMTL
Автор

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.

TheOnlyOwnedbyCow
Автор

Why you constantly uses the "var" variable? Is for to performance of the game?

TheXdestruidor
Автор

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.

luansantos
Автор

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.

captainawesome
Автор

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?

zidlik
Автор

Keep up the awesome content. Animating the character next would be awesome!

orchard
Автор

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!

tientam
Автор

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!

colorbreakers
Автор

right at the point! Fantastic! thx <3

beekall
Автор

Great tutorial man! i was wondering if its possible to move the player toward to mouse? as well with moving with the wasd keys?

vetirs
Автор

The package doesnt exist for me even when i hit show preview packages...

ntkn
Автор

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
Автор

Hey Thnaks a lot :) Just a question is it possible to make possible that where your mouse is, this is the forward? Thx :)

Philous