THIRD PERSON MOVEMENT in Unity

preview_player
Показать описание
Let's learn how to make a solid third person controller with a moving camera!

Jason no longer offers the course mentioned in the video.

····················································································

········································­­·······································­·­····

► All content by Brackeys is 100% free. We believe that education should be available for everyone.

········································­­·······································­·­····

♪ "ES_Dress Code_Black - oomiee" by Epidemic Sound
Рекомендации по теме
Комментарии
Автор

Bless your soul Brackeys! I miss you evey day

michaelanderson
Автор

I'm extremely appreciative of the fact that he made the code with the intention of being open for customization instead of being specific and restrictive. Literally a perfect video for a basic third person controller.

robotman
Автор

To lock the cursor, use the line 'Cursor.lockState = CursorLockMode.Locked;' in the start function.

Phoenix-
Автор

I can already see my brand new unfinished project

Edit:
I did not expect a 1000+ likes. Wow!

Morphinias
Автор

I was able to watch this video 14 times while waiting for Unity to load...

SpaceflightRocketShorts
Автор

tip: if u dont want ur camera to move up and down(y-axis) go to y axis in cinemachine and set the speed to 0 :). hope it helps!

Demon.Immortalis
Автор

If anyone is too lazy to write the code themselves, or just don't know how to do it; Here is my script which includes gravity, jumping, and everything shown in this video. You can also tweak it however fits your game :D

(Press read more)


using System.Collections;


using System.Collections.Generic;
using
using
using System.Threading;
using UnityEngine;

public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;

public float speed = 6;
public float gravity = -9.81f;
public float jumpHeight = 3;
Vector3 velocity;
bool isGrounded;

public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

float turnSmoothVelocity;
public float turnSmoothTime = 0.1f;

// Update is called once per frame
void Update()
{
//jump
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}

if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
//gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//walk
float horizontal =
float vertical =
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);

Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
* speed * Time.deltaTime);
}
}
}

aaronkanaron
Автор

Book Mark
2:06 Start (The course sounds great!)
4:38 Free Look Camera
17:37 Camera clip

Chiu-Pin
Автор

Me: making a race car game
Brackeys: *we can think of the character controller as the motor that drives the player*
Me: perfect.

SpaceflightRocketShorts
Автор

This came out at the perfect time. Had the idea for a third person shooter about three weeks ago, struggled to find code that I could easily add functionality on top of, and then I found this video. I've been coming back to it ever since then! Thank you!

CP_Maverick
Автор

For anyone looking for the Cinemachine dropdown menu, it is now accessed by right-clicking in the Hierarchy tab. If you do this and Cinemachine is not present down the bottom, try right-clicking inside your Project tab and clicking Reimport All.

quoipi
Автор

If any of you are using the High Definition template made by unity, and when testing game you may come along a message in the games window, try adding in the HD aditional camera data component.

masterbuildertristan
Автор

Please note, changing the camera near clip plane can really create a lot of Z fighting in the distance, or if you go to very extreme numbers, even in the middle distance.

The reason is how floating points and the Z-buffer works, the Z-buffer stores the pixel depth as a number between 0 and 1, with a bias towards the numbers near the camera (it's non-linear, so 0.1 is not 10% through the distance between near and far clip, but much closer to the camera). And then ofc, the floating point can only store a certain amount of precision, you don't have infinite memory to write down all the decimal points after all.

Practically this means that most of the Z-buffer is reserved for close to the camera, and by making the near value very small you basically make it so the difference between 100f and 101f is 0f, meaning objects far enough away will start occupying the same distance as far as the Z-buffer is considered.

In general you want your near clip plane to be the highest you can tolerate in the game, and if you reduce it with an order of magnitude you *should* reduce the far clip with the same magnitude. So from 0.3 - 1000 to 0.03 - 100.

Though I noticed now that Unity actually prevents you from going under 0.01 as near clip plane to avoid people setting it to 1e-10 and having everything just go wrong. Though you can set the far distance higher to see the effect partially, the near clip plane is more sensitive though due to the non-linear math being used.

Sylfa
Автор

Me : Works for hours writing camera collision code.

Cinemachine: Does it in two clicks.

Me: 0_0

andrewericliu
Автор

Fyi in Unity 2019.4 "All Packages" has been Renamed to "Unity Registry"

laurenwilkerson
Автор

I’m glad to see people replying to people who need help, it shows how great a community Brackeys built sad to see him go

samchill
Автор

As always, so much quality in the videos! Never gets boring to learn from the Brackeys team!

lucasbonde
Автор

I'm woking on a third person game with Legos, and this saved me from quitting, I'm serious, I was gonna stop making the game! TYSM, Brackeys! 😊

FaithBasedBiz
Автор

Aah, the infamous cilinder-cube man, ready to boldly go where none have gone before!

JelleVermandere
Автор

This is fantastic! I don't think anyone has videos quite like yours. I love how you perfectly balance getting to the point and explaining what's going on. Please keep making videos like this!!!

butlerfuqua