THIRD PERSON MOVEMENT in Unity

THIRD PERSON MOVEMENT in Unity

Brackeys

4 года назад

1,464,035 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Gulakov
Gulakov - 07.09.2023 03:12

adding a rigid body just breaks everything for me. what do i do?

Ответить
GamerBoss 8489
GamerBoss 8489 - 05.09.2023 01:11

I never understood atan2 before taking trigonometry class, but now it all just clicked!

Ответить
Cobra goldy
Cobra goldy - 04.09.2023 17:40

script if you are lazy


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

public class Playermovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
float turSmoothVelocity;

// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("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 turSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);

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

Ответить
The New Gamer
The New Gamer - 04.09.2023 17:07

does this work these days even when brackey's gone?

Ответить
Exile of the Mainstream
Exile of the Mainstream - 01.09.2023 01:29

CODE:


// Using Character controller component and using old input method

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

public class ThirdPersonMovement : MonoBehaviour
{

public CharacterController controller;
public Transform cam;
public float speed = 6f;

public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;

// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("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;
controller.Move(moveDir.normalized * speed * Time.deltaTime);

}
}
}

Ответить
Nick Dawson
Nick Dawson - 29.08.2023 11:25

the code did not work😭

Ответить
Koen van Wandeloo
Koen van Wandeloo - 28.08.2023 19:39

If I walk to the side my camera will keep looking at my character instead of following it, resulting in the character walking circles around the camera.
Does anyone know how I can fix this?

Ответить
WhiteGemDev
WhiteGemDev - 28.08.2023 13:23

Too sad you stopped making videos here, I still watch your stuff on a regular basis. Thanks for creating!

Ответить
ThatLazyGameDev
ThatLazyGameDev - 27.08.2023 13:55

anyone have ideas on how to add physics so the player stays on ground?

Ответить
Greate Britain
Greate Britain - 26.08.2023 17:06

Do someone wanna write me a five pages essay how is this movement script working, because Im kinda dumb to understand it.

Ответить
SAMEER.BHARDWAJ
SAMEER.BHARDWAJ - 25.08.2023 07:46

For, The new programmers watching this, IF your mouse direction is inverted, Go to cinemachine controller > x axis > turn off invert input

Ответить
AlbertDoesYT
AlbertDoesYT - 24.08.2023 01:38

Can someone help, whenever I move my mouse cursor to the left it always moves my camera so it is looking to the right. I have tried inverting everything that has to do with camera movement by adding negatives, and multiplying by negative 1 but nothing works. If someone could please help it would be greatly appreciated.

Ответить
Xusee
Xusee - 22.08.2023 18:48

Weirdly enough my player started facing a different direction than they are travelling, anyone can help with this?

Ответить
Felix Jefferies
Felix Jefferies - 15.08.2023 17:37

Best
EEEVVERERR

Ответить
Chronoverse
Chronoverse - 11.08.2023 22:01

thank u im trying to make a 3rd person platformer

Ответить
Ando
Ando - 10.08.2023 11:05

ChatGpt gave me a similar code when asking for a third person movement code.

Ответить
i have no idea how to call myself
i have no idea how to call myself - 09.08.2023 15:20

This is very helpful but how do I implement gravity for the player into this script

Ответить
Jonathan Garcia Rodriguez
Jonathan Garcia Rodriguez - 02.08.2023 05:29

I really wish you were a professor at my university

Ответить
Julian
Julian - 31.07.2023 14:19

Why is my Mouse movement for the Camera inverted?? And how can i fix that?

Ответить
m. a
m. a - 23.07.2023 19:39

Hi, how can i import Jump in this codes?

Ответить
Gabriel Astudillo
Gabriel Astudillo - 22.07.2023 02:11

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

public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float Speed;
public float turnSmoothTime;
private float turnSmoothVelocity;
public Transform cam;
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("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(0,targetAngle,0f) * Vector3.forward;

controller.Move(moveDir.normalized * Speed * Time.deltaTime);
}
}
}

Ответить
K4Kadyan
K4Kadyan - 21.07.2023 11:05

He is noob in teaching mf

Ответить
Deleted
Deleted - 12.07.2023 15:24

i was searching for ages how to make san andreas camera thanks

Ответить
Random rat
Random rat - 11.07.2023 11:33

for the script it wont let me add the character controller as the controller

Ответить
dx levou
dx levou - 10.07.2023 10:32

Thankyou bro❤❤

Ответить
ABHINAVA KARTHIKEYA
ABHINAVA KARTHIKEYA - 06.07.2023 19:57

no one did..so iam doing it.....

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

public class controllerscript : MonoBehaviour
{

public CharacterController controller;
public Transform cam;
public float speed = 6f;

public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;

// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("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;
controller.Move(moveDir.normalized * speed * Time.deltaTime);

}
}
}

Ответить
Тимур Галлямов
Тимур Галлямов - 05.07.2023 14:20

For all lazy programmers:

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

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

public float speed = 6;

public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;

void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;

if (direction.magnitude >= 0.1f)
{
isMoving = true;

float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref smoothTurnVelocity, smoothTurnTime);
transform.rotation = Quaternion.Euler(0, angle, 0);

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

Ответить
Wylie
Wylie - 30.06.2023 01:38

I'm having a hard time trying to implement the ability to JUMP, while using this script. Any help would be much appreciated!

Ответить
NoobyManFace
NoobyManFace - 29.06.2023 22:08

Wait but how can we code the jump??

Ответить
Marc Pada
Marc Pada - 26.06.2023 01:58

Amazing tutorial really easy to understand love this and you❤❤❤

Ответить
Malik Muhammad Arslan
Malik Muhammad Arslan - 11.06.2023 18:29

Legend Brackeys

Ответить
Megatreeboi😐
Megatreeboi😐 - 07.06.2023 01:05

did not work

Ответить
NishantRishi Nair
NishantRishi Nair - 06.06.2023 14:24

! I was trying to make a third person camera system for my game but it wasn't really working. Despite the differences in setup, this worked for me without having to change much. Thanks for making this tutorial so flexible/customisable

Ответить
James Mcafee
James Mcafee - 19.05.2023 01:56

Can you send me a link to your third person movement script pleas mine isn’t working I’ve follows step by step but still nothing

Ответить
Maxime Marquis
Maxime Marquis - 15.05.2023 19:50

im too lazy too rewrite the script and oh hell nah im not downloading that. can someone please rewrite it so i can copy paste?

Ответить
VANPARIYA DEEP
VANPARIYA DEEP - 13.05.2023 16:28

it is going forwerd when i jump.

Ответить
Zinrith Tirngate
Zinrith Tirngate - 07.05.2023 04:33

bean

Ответить
CodeCrafters
CodeCrafters - 25.04.2023 17:16

there was less space in room so he just rome around the table lol

Ответить
CodeCrafters
CodeCrafters - 25.04.2023 17:15

room me jagye kam pas rahi thi to aak table ka hi chakar kat liya

Ответить
futureGM
futureGM - 25.04.2023 16:19

the camera direction while on play mode are inversed can some one help?

Ответить
YUG SINGH
YUG SINGH - 22.04.2023 11:47

my unity is telling to Assets\ThirdPersonMovement.cs(20,64): error CS1002: ; expected
pls help heres my scipt

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

public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;

public float speed = 6f;

// Update is called once per frame
void Update()
{
float Horizontal = Input.GetAxisRaw("Horizontal");
float Vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(Horizontal, 0f, Vertical).normalized;

if (direction.magnetude >= 0.1f)
{
controller.move(direction * speed * Time.deltaTime)
}
}
}

its the same from tutorial
🥲

Ответить
Burak Herdem
Burak Herdem - 16.04.2023 05:40

This tutorial was so good that I could feel the third person movement coming inside me whenever i played my game

Ответить
outhen
outhen - 14.04.2023 20:27

thanks done the turorial

Ответить
Pro
Pro - 10.04.2023 04:23

hippity hoppity your code is now my property

Ответить
bist Bist
bist Bist - 09.04.2023 11:42

you're great

Ответить
ZephyrMods
ZephyrMods - 01.04.2023 16:30

when making to cinemamachine nothing worked for me. any help?

Ответить
Err0r
Err0r - 27.03.2023 21:18

unity is giving me this code: Assets\thirdpersonmovement.cs(21,78): error CS0117: 'Mathf' does not contain a definition for 'Rad2deg'
anyone know how to fix this? please help

Ответить