How to Create Player Movement in UNITY (Rigidbody & Character Controller)

How to Create Player Movement in UNITY (Rigidbody & Character Controller)

Rytech

3 года назад

115,478 Просмотров

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


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

@jordanbentley
@jordanbentley - 17.12.2023 23:35

thanks, youre a legend mate

Ответить
@user-hb8tq6xi4q
@user-hb8tq6xi4q - 07.07.2023 00:34

I was trying to get this to work but the gravity and speed and etc. weren't able to be changed using the Character controller script all time see is it saying that its a script

Ответить
@penguinking2984
@penguinking2984 - 03.07.2023 09:17

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

public class RigidbodyMovement : MonoBehaviour
{

private Vector3 PlayerMovementInput;
private Vector2 PlayerMouseInput;
private float xRot;

[SerializeField] private LayerMask FloorMask;
[SerializeField] private Transform FeetTransform;
[SerializeField] private Rigidbody PlayerBody;
[SerializeField] private Transform PlayerCamera;
[Space]
[SerializeField] private float Speed;
[SerializeField] private float Sensitivity;
[SerializeField] private float JumpForce;

void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}



// Update is called once per frame
void Update()
{
PlayerMovementInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
PlayerMouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

MovePlayer();
MovePlayerCamera();

}

private void MovePlayer()
{
Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput) * Speed;
PlayerBody.velocity = new Vector3(MoveVector.x, PlayerBody.velocity.y, MoveVector.z);

if (Input.GetKeyDown(KeyCode.Space))
{
if (Physics.CheckSphere(FeetTransform.position, 0.1f, FloorMask))
{
PlayerBody.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
}
}

}

private void MovePlayerCamera ()
{
xRot -= PlayerMouseInput.y * Sensitivity;
xRot = Mathf.Clamp(xRot, -90f, 90f);


transform.Rotate(0f, PlayerMouseInput.x * Sensitivity, 0f);
PlayerCamera.transform.localRotation = Quaternion.Euler(xRot, 0f ,0f);
}
}

Ответить
@IAmStickyWicky
@IAmStickyWicky - 06.06.2023 10:44

For anyone who has a problem with moving diagonally doubling movement speed, locate the following line within your MovePlayer() function:
Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput);
Now replace it with this line:
Vector3 MoveVector = transform.TransformDirection(Vector3.ClampMagnitude(PlayerMovementInput, 1f));
This let's us set the maximum magnitude that PlayerMovementInput can have to equal 1.
Explanation:
PlayerMovementInput the way we've defined it, gets its magnitude value from our input axis. This means that when we press W, the vector would have a value of 1 in the z direction. The problem comes when we press two keys at once, like W and D. Now the vector's value will be 1 in the z direction PLUS 1 in the x direction, doubling our intended value for PlayerMovmentInput and by extension, MoveVector when it is used elsewhere. Vector3.ClampMagnitude(*your Vector3 here*, *your value here*) lets us keep the direction component of the vector, while limiting the magnitude to whatever value we choose, in this case 1.
One alternative solution would be to normalize PlayerMovementInput like so:
Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput.normalized);
What this does is set the magnitude of our Vector3 to 1. That's it. It's either nothing, or 1. This works, but it loses the smoothing effect of the Input.GetAxis we used to get our inputs, making movement jerky and unpleasant. I wouldn't recommend it in this instance.

Ответить
@robertgordon7724
@robertgordon7724 - 05.06.2023 17:01

If the script from the outside gives the player a velocity (AddForce X, Z), it will not work on him. Bad example

Ответить
@talismanskulls2857
@talismanskulls2857 - 31.03.2023 08:23

Thanks for posting this. Maybe you can help me sort an issue I am having. I got my character moving and all like I wanted but when i try to use it (rigidbody) to detect and use steps, it stalls, bounces or get stuck to things when jumping, even when i have zero friction physics material on something like a wall collider.

Also be i try to make it move backwards with the cam i been working on, something with the camera stops it from backing up and makes it skip forward. The code for the cam is the biggest of the two scripts.

Ответить
@UrlocalBagel
@UrlocalBagel - 31.03.2023 07:42

i really really REALLY HATED THIS VIDEO IT DID NOT HELP ME AT ALL

Ответить
@scottmedhaug4107
@scottmedhaug4107 - 14.03.2023 10:24

Nice Job

Ответить
@stickman2051
@stickman2051 - 10.03.2023 04:11

Extremely helpful! I was looking forever for a tutorial 3rd person with only rigidbody... Thanks! 😊

Ответить
@panickal
@panickal - 01.03.2023 19:47

I don't like that this changes the velocity directly instead of using a force.

Ответить
@YamomotoSC2TV
@YamomotoSC2TV - 27.02.2023 17:30

WHY serialized field float? etc
type just public float
LOL wtf ???

Ответить
@MerrStudio
@MerrStudio - 19.02.2023 23:59

Do you know why rotate is moving the object instead of rotating it on my side?

Ответить
@atsu5411
@atsu5411 - 04.02.2023 04:32

Great video

Ответить
@kiwanexe
@kiwanexe - 06.01.2023 17:37

the charater controller movement script doesnt work, the thing where i put the values in doesn show it just says (Script)

Ответить
@monkeysarestinky3106
@monkeysarestinky3106 - 06.01.2023 08:57

Thanks!

Ответить
@YapGD
@YapGD - 18.12.2022 17:58

what about mathf.clamp ?

Ответить
@austinthesavage745
@austinthesavage745 - 09.12.2022 03:57

nothing showed up on my script and some of the code was different from yours

Ответить
@austinthesavage745
@austinthesavage745 - 09.12.2022 03:37

i dont see a character controller script?

Ответить
@odinfrodin2676
@odinfrodin2676 - 07.12.2022 22:42

I have had some problems whit the rigidbody controller so thanks grate vid!!

Ответить
@sethreed7644
@sethreed7644 - 06.12.2022 21:01

i dont know what i did wrong but my character can only move left and right

Ответить
@imibuks-replit
@imibuks-replit - 09.11.2022 17:05

Source code?

Ответить
@imibuks-replit
@imibuks-replit - 09.11.2022 16:54

HI thanks alot
I was searching google, ~coping~ inspiration Ing code, and everything gave some stupid error
This was really helpful, followed along with the video and it is pretty good. Not the world's best character controller but good enough for me, starting out with unity.

SO Thanks

Ответить
@paavoalakoski1094
@paavoalakoski1094 - 26.10.2022 19:39

Whenever I bump into something my character starts slowly spinning on the y axis?

Ответить
@ColdFoundX
@ColdFoundX - 16.10.2022 19:31

does somebody know how to disable looking up and down???

Ответить
@excaliber_bruh
@excaliber_bruh - 08.10.2022 06:26

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

public class RigidbodyMovement : MonoBehaviour
{

private Vector3 PlayerMovementInput;
private Vector2 PlayerMouseInput;
private float xRot;

[SerializeField] private LayerMask FloorMask;
[SerializeField] private Transform FeetTransform;
[SerializeField] private Rigidbody PlayerBody;
[SerializeField] private Transform PlayerCamera;
[Space]
[SerializeField] private float Speed;
[SerializeField] private float Sensitivity;
[SerializeField] private float JumpForce;

void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}



// Update is called once per frame
void Update()
{
PlayerMovementInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
PlayerMouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

MovePlayer();
MovePlayerCamera();

}

private void MovePlayer()
{
Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput) * Speed;
PlayerBody.velocity = new Vector3(MoveVector.x, PlayerBody.velocity.y, MoveVector.z);

if (Input.GetKeyDown(KeyCode.Space))
{
if (Physics.CheckSphere(FeetTransform.position, 0.1f, FloorMask))
{
PlayerBody.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
}
}

}

private void MovePlayerCamera ()
{
xRot -= PlayerMouseInput.y * Sensitivity;
xRot = Mathf.Clamp(xRot, -90f, 90f);


transform.Rotate(0f, PlayerMouseInput.x * Sensitivity, 0f);
PlayerCamera.transform.localRotation = Quaternion.Euler(xRot, 0f ,0f);
}
}

Ответить
@Tanklover99
@Tanklover99 - 06.10.2022 15:36

I was halfway through the code when I realized this was a third person camera (I'm making a first-person game)

Ответить
@bonesmcgeefr
@bonesmcgeefr - 17.09.2022 20:01

does not work. i dont know why but the vertical movement does not work and my player just flips in the air. i have tried constraints and even making a frictionless surface but it just flips around and doesnt move forward or backwards at all.

Ответить
@cejjy
@cejjy - 17.09.2022 03:23

@Rytech so for some reason my a and d keys switch during movement, any idea?

Ответить
@zibozhao5789
@zibozhao5789 - 21.08.2022 04:55

nice tutorial !!!

Ответить
@WatchMeFail69
@WatchMeFail69 - 15.08.2022 16:06

nothing work i only get erorrs plus this happens on every tutorial i watch

Ответить
@NarutoUzumaki-gj9zn
@NarutoUzumaki-gj9zn - 08.08.2022 14:56

to many errors L vid

Ответить
@arminrad5357
@arminrad5357 - 07.08.2022 13:15

amazing

Ответить
@Caliper_Click
@Caliper_Click - 02.08.2022 23:17

I use rigidbody and when i move my camera horisontally while straifing my player shake seriously, resulting in a very bad visual effect(
i used void update instead of fixedupdate (fixed makes camera rotate at 50fps while the game is at 500) and normalised movement vector with deltatime.
also i use rigidbody.MoveRotation(rigidbody.rotation * quaterion (it is Quaternion.Euler(0f, PlayerMouseInput.x* mouseSensivity, 0f)) so it is a bit less shaky but still not good

Ответить
@Oreo-dude
@Oreo-dude - 31.07.2022 00:25

Why is Character Controller not going into the Controller thing where you have to add all the variables at the end help please i get drag the character controller variable into the controller thingy Please i need heplepwktkppppppppppppppppp 😁😁😁😁😁😁😁😁😁😃😃😀🤣😉🤗🤗🤗😬😬😬😬😬😬😬😬😬😬😬😬😬😬😔😪😪😪😑😐😐😐😐😐😐😐😐😐😐😐😐😐😛😛😛😛😛😊😊😊😊😊😊😴😴😴😴😴😴😴😴😴😷😷😷😷😷😷😷😷😷😷😷😷😷😷😷😷😷🥵🥵🥵🥵🥵🥵🥶🥶😌😌🥶🥶🥶🥶🥶🥶🥶👺👺👺👺👺👺👺👺👺👺👺👺👺👺👺👺🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤💟🖤🖤🖤🖤💙💙💙🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤💙💙💙🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤💙💙💙🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤

Ответить
@mentallyunprepared8635
@mentallyunprepared8635 - 28.07.2022 14:04

if anyone had problems with the feet "isGrounded", like I had. Try making the radius smaller e.g. " Physics.CheckSphere(FeetTransform.position, 0.001f , defaultMask "

Ответить
@justusisbestest4122
@justusisbestest4122 - 25.07.2022 19:34

thank you so much

Ответить
@ProjectEAUX
@ProjectEAUX - 19.07.2022 02:21

Spent Six Hours Copying over the script but otherwise great Giude!

Ответить
@unitycc5499
@unitycc5499 - 08.07.2022 18:58

ESTOS ERRORES COMETIEN EL TUTORIAL:
- No me fige en los " -= " por lo lo que no me daba resultados
-- No agregue "
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
"
gran tutorial thanks

Ответить
@LeonTVRyt
@LeonTVRyt - 06.07.2022 02:36

I wonder why this isn't built in it takes a really long time to do so why isn't it built in?

Ответить
@Ksungaa
@Ksungaa - 03.07.2022 19:55

Thsnks for video i needed the help!!!! 🙌🏾

Ответить
@vo1dzthedev
@vo1dzthedev - 03.07.2022 16:20

thank you soo much im new to unity and i just learned this rom you! i subbed

Ответить
@AryanSingh-us5ej
@AryanSingh-us5ej - 27.06.2022 22:58

I exactly looking for this since a Day, But at last i found you so this made my Day : )

Ответить
@s_tupid1256
@s_tupid1256 - 27.06.2022 07:39

hey ya can i just have the code my brain is frying at the sight of this code all i know how to do in unity is make simple forward backward pan left and pan right and maybe rigid body physics with it too

Ответить
@extracheesyplayer2192
@extracheesyplayer2192 - 23.06.2022 21:50

COULD YOU ATLEAST PUT THE SCRIPT IN THE DESCRIPTION?

Ответить
@mibo363yt5
@mibo363yt5 - 11.06.2022 20:04

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

public class RigidbodyMovement : MonoBehaviour
{

private Vector3 PlayerMovementInput;
private Vector2 PlayerMouseInput;

[SearailizeField] private Transform PlayerCamera;
[SearailazeField] private Rigidbody PlayerBody;
[Space]
[SearailazeField] private float Speed;
[SearailazeField] private float Sensitivity;
[SearailazeField] private float Jumpforce;

private void Update()
{
PlayerMovementInput = new Vector3(Input.GetAxis("Horizontal"), 0f, PlayerMovementInput.GetAxis("Vertical"));
PlayerMouseInput = new Vector2(Input.GetAxis("Mouse X"), PlayerMouseInput.GetAxis("Mouse Y"));

MovePlayer();
MovePlayerCamera();
}

private void MovePlayer()
{
Vector3 MoveVector = transform.TransformDirection(PlayerMovementInput) * Speed;
PlayerBody.velocity = new Vector3(MoveVector.x, PlayerBody.velocity.y, MoveVector, z);

if (Input.GetKeyDown(KeyCode.Space))
{
PlayerBody.AddForce(Vector3.up * Jumpforce, ForceMode.Impulse);
}
}

private void MovePlayerCamera()
{

}

}

Ответить