First Person Movement in Unity



 So, you're back now our real game development begins so we'll make a first-person character like games like Fallout and fire watch. These kinds of games are usually good so without any delay let's start it, people.

Step 1: Creating a Platform

So now we will create a platform where the player can stand and move. So to do that, create a cube and do as given in the figure:


figure 1.1


Step 2: Adding a player

So ok we've added a platform for the player to move now we'll add player since we can't see our body in first-person games we'll just add a capsule and name it player as of figure 1.2

                                              figure 1.3

Ok after you do this move the player camera to the player easily by pressing ctrl +shift +f and make it as a child of the player as given in the image below.



                                                       figure 1.3

Step 3: Adding Character Controller Component

Now you need to add character Controller Component. It helps player movement be easy and smooth to add, select the GameObject on which you want to add it, and do the same as figure 1.4. 
                                                                    figure 1.4

Step 4: Creating a script.

Now after this create a script and name it as 'PlayerMovement' and Open it in your script editor and type the following code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]

public class PlayerMovement : MonoBehaviour
{
    public float walkingSpeed = 7.5f;
    public float runningSpeed = 11.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Camera playerCamera;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;

    CharacterController characterController;
    Vector3 moveDirection = Vector3.zero;
    float rotationX = 0;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
        characterController = GetComponent<CharacterController>();

        // Lock cursor
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        // We are grounded, so recalculate move direction based on axes
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.TransformDirection(Vector3.right);
        // Press Left Shift to run
        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
        float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
        float movementDirectionY = moveDirection.y;
        moveDirection = (forward * curSpeedX) + (right * curSpeedY);

        if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
        {
            moveDirection.y = jumpSpeed;
        }
        else
        {
            moveDirection.y = movementDirectionY;
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        if (!characterController.isGrounded)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        // Player and Camera rotation
        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);
        }
    }
}

Step 5: Adding Script to player

Now you have made a script so what you need to do is just put the script component in the player and assign the values to do that, see figure 1.5.

figure 1.5

Done!

By doing this, we get the following result.



Thanks for reading this I hope that you learned something you don't need to pay me but if u wish to donate then, donate at this PayPal account: sanjeevbshah@gmail.com.

Comments

Popular posts from this blog

Unity Rigidbody Movement

How To Make A Game