UNITY 3D PLAYER MOVEMENT in 2 MINUTES! FPS Shooter

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


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 =
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}

void Update()
{
Vector3 forward =
Vector3 right =

bool isRunning =
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 &&
{
moveDirection.y = jumpPower;
}
else
{
moveDirection.y = movementDirectionY;
}

if
{
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;
}

* Time.deltaTime);

if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
= Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}

Brogrammer
Автор

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.

oryxdotpng
Автор

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

acceleratingthesupernatural
Автор

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.

kutlutortop
Автор

This was really helpful, I couldn’t find a tutorial that I actually understood until now

BananaPuddingMuntjac
Автор

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

hepona
Автор

Everything worked fine except the fact that My character decided just not to obey the laws of physics and follow through the ground so EDIT THANK YOU FOR 38 LIKES

Redblocks_
Автор

Straight to the point no advertisement best blender video ever

firefriends
Автор

This is a really good video it helped me

Redblocks_
Автор

bro thank you so much i wouldve never been able to do anything ever and wouldve deleted unity immediately if i didnt have this

fireblaze
Автор

I HAVE BEEN SEARCHING THROUGH SO MANY TUTORIALS AND UR IS THE FIRST TO ACTUALLY

ReedFootball
Автор

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

Redblocks_
Автор

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

spaghettiHell
Автор

hey, i wanted to thank you. i needed this for a schoolproject i need to finish next week. thanks

Laggyboiii
Автор

does not work when I put the script on the player it does not show any thing to attach my camera too

diamondarmorkid
Автор

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!

cammyposey
Автор

Learning the basics and watched many videos and this worked perfectly thank you!!

RealGhsty
Автор

i have been using this tutorial for like a year and it still works thank you!!!

danielandhenry
Автор

Thanks bro finally after 2 days I got a perfect tutorial love from india🇮🇳🇮🇳🇮🇳

I.m.trigno
Автор

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?

VkStudio_