ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
UNITY 3D PLAYER MOVEMENT in 2 MINUTES! FPS Shooter
ฝัง
- เผยแพร่เมื่อ 10 พ.ค. 2023
- Walk, Run, Jump and Sprint! Easily customizable!
A very simple player movement script that will get you started on your 3d project. Code is pinned in comments.
3D, First person (easy change to third person).
Sub for more :)
Here is the script. If you are confused please let me know!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
public Camera playerCamera;
public float walkSpeed = 6f;
public float runSpeed = 12f;
public float jumpPower = 7f;
public float gravity = 10f;
public float lookSpeed = 2f;
public float lookXLimit = 45f;
public float defaultHeight = 2f;
public float crouchHeight = 1f;
public float crouchSpeed = 3f;
private Vector3 moveDirection = Vector3.zero;
private float rotationX = 0;
private CharacterController characterController;
private bool canMove = true;
void Start()
{
characterController = GetComponent();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpPower;
}
else
{
moveDirection.y = movementDirectionY;
}
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
if (Input.GetKey(KeyCode.R) && canMove)
{
characterController.height = crouchHeight;
walkSpeed = crouchSpeed;
runSpeed = crouchSpeed;
}
else
{
characterController.height = defaultHeight;
walkSpeed = 6f;
runSpeed = 12f;
}
characterController.Move(moveDirection * Time.deltaTime);
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
baller
THANK YOU!!!
❤🎉
ConsoIe says playerCamera is not defined
Wait nevermind
This is the best coding tutorial I have gotten so far. When I first tried Unity, I was unable to comprehend the game engine and quit. Now, a few months later, I have a working 3D project. Thank you. So much.
You're very welcome!
@@Brogrammer1 Make an explanation video
Hay I know you posted this 2 months ago but I’m having a bit of trouble because when I add the script as a component it doesn’t add the character controller and the script shows but has no variables like “Camera” and “walk speed” it only has the name of the script.
same
Same
Sorry for the super late reply - Make sure the name of the script is the exact same as the one I used in the video. That could be the issue.
Ok because I’m having the same issue
@@Chris-Stikman I fixed the issue, you kinda just have to re-save the script until it works
Bro thanks so much I watched a lot of other videos and I made it for so long but it didn’t even work but thanks to you you even put the script in the commans
Everything worked fine except the fact that My character decided just not to obey the laws of physics and follow through the ground so
Add a rigidbody component to it. Should work fine
Cause you didn't add a capsule collider to it or maybe you didn't add a collider to your ground
Fr
😂
Finally a tutorial that works i love you bro keep up the good work.
Bro I fucking love you. I had spent half an hour battling with like 3 scripts for the alternative input manager from unity until I decided to scrap the entire movement system and look for another solution. Yours worked like a charm, you are a legend man
Same
Glad I could help :)
If your player is falling then click the player folder and a green line will show up. Click the capsule and move it up to the green line and it should work!
I love you, you are the best, YOU PUT THE SCRIPT DOWN, THE SCRIPT WORKS PERFECTLY, YOU SOLVED A 5 HOUR LONG PROBLEM IN SECONDS, YOU NEED ALL THE SUBS
FRRR
can someone help my floor keeps falling into the void and i can't find a tutorial to fix it i only started unity yesterday and if u can help the will me appreciated
I think that the floor doesn’t have collision
Idk how to add collision i just got unity today
EEK!! THANK YOU SO MUCH BRO, I LOVE YOU!! (not in a weird way)
This was really helpful, I couldn’t find a tutorial that I actually understood until now
Glad I could help
@@Brogrammer1 You earned a sub Thanks
It doesn’t work 😢
@@GKBGAMER9733 It does
if we just copy the code, that not mean we understand it
...
Thank you so Fuuuuging much bro easies tuturial ever
bro I can't add playermovement as component its show compile error please tell me how solve it i am beginner in unity 😥
same
@@Jr_radman-true 😭
Thanks for this! Worked completely fine.
no intro, no bullshit, just coding. Perfection
Thanks! So many tutorials but this one was the best!
HI
HOW TO TURN THE CAMERA SIDEWAYS
THank you so much i can a map but i dont know ho to make scrpt for walk soo thank you so much
Very good Tutorial easy to setup so customizable I subscribed btw :)
Thanks for the sub! I'm glad it works
Thank you so much! Worked like a charm!
Fucki** thx bro it’s like the 100th tutorial I do and 99th of them doesn’t work appreciating you sorry my English I’m French
I would like A kind of detailed tutorial that shows you how to like change the speed and jump height I like something like that
when i try to add the main camera to the script, i can't find it. its just script:player movement(name of the script)
how did you fix it
This is a really good video it helped me
Thanks for the help because I'm new to unity :)
You're welcome :)
its not working the script isnt giving me any extra options
bro thank you so much this really helped me
Glad it helped
I tried third person but the camera keeps on turning around when i start up the game and wont follow the player
hmm try to make another script with camera movement and delete the camera movement from the movement script and like just make a new one or search up a new script
thank you, Ive been serching for hours how to make my player move and youre tutorial was the only on that worked, thank you
Glad I could help!
There is a problem. I tell my PC to open the code in GOOGLE. But whats the problem? I cant change the script.
Nvm i figured it out
@@GamerGuy69-Q WHAT WAS IT
hey, i wanted to thank you. i needed this for a schoolproject i need to finish next week. thanks
A bit late, but im glad I could help :)
this is the PERFECT Tutorial its short and everything works perfect this is the first game i ever make and im so happy thx a lot
thank you brother i was struggling tryna learn the code but u made it so easy great to help me study and referance off of big thanks
First of all, THANK YOU SO MUCH. Besides this, could you please create more videos similar to this one? It's helped me a lot, and it would be great if you could help with other things as well
for some reason i have a bug, my player is moving without control, if you make a vid of how to fix that than you will get 10 subs (because i have this and other 9 devices)
Hey thank you! I used your code for my game to make snappy movement and its perfectly scalable and clean! I have some big ideas and needed a good movement base to work off of so thank you so much
Can you start a new Horror Game Tutorial series? Great to see a new developer, whom I can follow live. All the best!
I’m actually making a small one right now, it’s running out really fun
@@Brogrammer1 Oh, you're gonna upload tutorials too?
bro you are my hero. i have been stuck at the player movement for 4 years but finally i found a working tutorial. i love you so much
No problem!
hope you make a discord server one day im really strugglin with this shit so even just talking with someone that knows about it would help me a ton:D
Bro Your A Frickin LIFESAVER man, THANKS
No Problem!
I would suggest putting a collider on the camera so that you do not see through things
Thanks this helped me out a lot!
bro thank you so much i wouldve never been able to do anything ever and wouldve deleted unity immediately if i didnt have this
bro thanks so much it works perfectly and it is sick and easy bro keep the good work up
Happy to hear it works :)
First totorial that works. Thank you
No Problem!
I HAVE BEEN SEARCHING THROUGH SO MANY TUTORIALS AND UR IS THE FIRST TO ACTUALLY WOKR!!!!!!
I'm glad to hear it worked :)
yo thanks so much im new to unity and everytime i try to find a video it always doesnt work but this one does!
Thanks man this was a lot of help❤
you are a LIFE SAVER!!! i was so confused since i dont know c#, thank you a lot!
Thank u very much for this great tutorial :D
ur such a W bro, ive been looking for wasd controls since like, january, and this one finally worked, and i didnt expect it to only take 2 minutes, (i know it wouldve taken way longer if you didnt just let us copy the code, and instead walked us through the code like others)
Easiest and fastest fps view game controller thankyou so much.
thats why i love ctrl c and v
not only did this man make a 2 min tutorial straight to the point, but even put the script in the comments. Absolute chad. Keep up the great work. However im having a small problem. When i change the values of the Run speed & walk speed and then start the game the run speed resets to 12 and the walk speed resets to 6. How can i fix this?
Did you make sure that you edited the values with the play button unselected? If you change anything while in play mode (even if you also have it paused) it will reset.
Edit the code on lines 67 and 68. It says that if you are not crouching or sprinting, it will set these values to 6 and 12
Sorry for the late reply, instead of through unity, try changing the values through the actual script. Just look for where I have the walk and run speed set and change the values there. You can use control f to search for keywords.
Thank you wery much this tutorial was quick and clear
You're welcome!
Thanks so much for script, you just write my 1st script
Bro thank you, you helped so much!!!!
holyyyyy this is the tutorial that was the easiest to understand. also thank you so much for posting the code in the comments, I don't have to pause the video every 10 seconds to write the code and waste time. Instead i can look through it myself after it is in a working state.
I like to make my videos as easy as possible to follow :) glad I could help
Thanks! This tutorial was really helpfull for my game.
Great!
@@Brogrammer1 i can't rotate :(
can you help me?
Hi
HOW TO TURN THE CAMERA SIDEWAYS@@Brogrammer1
thanks a lot this helps:)
Bro im totally new to unity and this tutorial will help me with alot of games
it did work but For me I was not impressed I started to have problem like the camera not sticking to the player and even with colliders for my ground it still falls thought but kinda helpfull
You are a legend bro
VERY helpful and simple! Thank You for nailing this tutorial!
op bro good job and pls make the video how to make AI enemies in your game in unity
Sorry for the late response. I will work on that video soon! I just upload a working gun, so that could get you started on your ai journey :)
@@Brogrammer1 thanks bro for the Gun tutorial its really helpful
Support from India
I loved the video and it really helped me a lot! Thanks!
Glad it helped!
Hi I have a question Do you think you can make some more videos I watched both your videos and they were really cool and they helped me make my games can you make some more like maybe just 1 or 2 😊😊😊
very simple! TY
Thanks a ton brother, have been watching videos for more than an hour but couldn't move my player correctly but you gave the solution to my problem.
You got a new sub😊
Ik its been 1 yr but when I add the script to my character empty it makes the capsule fall threw the ground?
thank you this was super useful :)
Glad it was helpful!
Perfect job. Much more in detail unlike some other tutorials that already expect you to know stuff. I managed to make it with no issues. Thank you!
Really great tutorial! I can finaly start making stuff!
Great to hear!
great video man 👍
Appreciate it!
If the script don’t work just use chat for and say give me a script for walkibg
lifesaver!! please upload code a explanation video too. (+ you gained a subscriber)
Glad you liked it :)
when i drag it in it just dosent work
i had 3 errors while adding your script and i dont know how to fix them pls could u tell me how to the errors were Assets/PlayerMovement.cs(23,10: error CS1529: A clause must precede all other elements defined in the namespace exept extern allies declarations
the other 2 were the same (errors)
Ensure that the name of the file is the exact name I used in the video! Also copy the code directly from the comments and use that.
This help me a lot to make a game thanks
Awesome tutorial. Everything works except the variables don't save when changed in the inspector? I'll change run and walk to 4 and 8 for example, go to test play and it automatically resets them to 6 and 12.
Can u do another version talking slower I have to pause every second but this is still helpful
this script is not working for me
hang on, let me launch unity!
THANK U SO MUCH MAN THIS IS MY LIKE 20TH TUTORIAL THAT I TRIED AND IT FINALLY WORKS THANKS
Haha, glad I could help!
Thats literally the best video tutorial about fps control
Glad you liked it :) More videos coming up as well
when i put the script into the player folder/ inspector side it just says default-material (Material)
this helped so much highly suggest!
hippity hoppity, this code is my property (this tut actually helped a lot thanks mate)
No problem :)
Thank u man!!!
No Problem!
PLEASE KEEP UPLOADING
Just uploaded another video - and more to come :)
YUR THE BESTTT
the things is wrong
Thanks for being efficient, but honestly you could use slowing down and explaining a little bit, you literally just skip through coloring the blocks and how you do it in less than 2 seconds while you explain something unrelated
That's problematic for a tutorial when you have to slow down to quarter speed and still have to guess work what fucking key combo you use
Well... It sort of works. For me it doesn't turn or jump. It is first person but you cant do anything. When i press the play button it immiedelty falls through everything undergound
for me it falls through
Can you please help me ive been trying for ages and ive restarted the project loads of times but nothing works. At 1:40 when i drag and srop it there is no option for the player camrea and its just all greyed out please help ive looked at multiple videos but its just the same issue.
nvm i got it working
@@Wookie-1how did you fix it
@@Wookie-1 thats happening to me how did you fix it?
@@esxcape4756 me too its so annoying
The script doesn't work for me. :(( Any tips? I did absolutely everything you said. Thank you!
Great Brother ! .i was Looking For the Perfect Movement Script ,!
Glad it works :)
For me it falls through the terrain even tho i have colliders for both they are not trigger (Fixed it i deleted the empty gameobject called player then i added the script to my player (game object)) (Fixed it)
Glad it hear its working now 👍
When i run the scene i instantly clip through the plane =/
I've got a question, though I'm not sure if you'll answer given this is a 9 month old tutorial. The movement works overall, but I find that when moving diagonally the speed is increased. Do you have any idea on how to fix this?
don't fix it, it's a really known thing on movement systems, games like minecraft and karlson have it. it is not uncommon at all
@@gokete3555 Thing is, i don’t really want that in my game
@@TMP5_xd no one will notice that
@@Dylanj8795 They almost certainly will
@@TMP5_xd I have been playing games for a really long time now and I have never noticed that so….