banner



How To Make A Smooth First Person Camera In Unity

FPS (or First-Person Shooter) is a type of game where the chief character is controlled from a commencement-person perspective.

With usual controls being W, A, S, D to walk, Mouse Look to look around, Infinite to jump, and Left Shift to dart, assuasive the player to freely move around the level.

In this mail service, I volition be showing how to brand an FPS controller in Unity that volition handle photographic camera rotation and role player movement.

Steps

To make an FPS controller, follow the steps beneath:

  • Create a new Game Object (GameObject -> Create Empty) and proper noun information technology "FPSPlayer"
  • Create new Capsule (GameObject -> 3D Object -> Capsule) and motion it within "FPSPlayer" Object
  • Remove Capsule Collider component from Capsule and change its position to (0, 1, 0)
  • Motion the Main Camera inside the "FPSPlayer" Object and change its position to (0, one.64, 0)

  • Create a new script, name it "SC_FPSController" and paste the lawmaking below inside it:
          using System.Collections; using System.Collections.Generic; using UnityEngine;  [RequireComponent(typeof(CharacterController))]  public class SC_FPSController : MonoBehaviour {     public bladder walkingSpeed = vii.5f;     public float runningSpeed = 11.5f;     public bladder jumpSpeed = 8.0f;     public float gravity = 20.0f;     public Camera playerCamera;     public bladder lookSpeed = 2.0f;     public float lookXLimit = 45.0f;      CharacterController characterController;     Vector3 moveDirection = Vector3.zip;     float rotationX = 0;      [HideInInspector]     public bool canMove = true;      void Get-go()     {         characterController = GetComponent<CharacterController>();          // Lock cursor         Cursor.lockState = CursorLockMode.Locked;         Cursor.visible = false;     }      void Update()     {         // We are grounded, then recalculate move direction based on axes         Vector3 forwards = transform.TransformDirection(Vector3.forward);         Vector3 right = transform.TransformDirection(Vector3.right);         // Printing Left Shift to run         bool isRunning = Input.GetKey(KeyCode.LeftShift);         bladder curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;         float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;         float movementDirectionY = moveDirection.y;         moveDirection = (frontward * 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 considering gravity should exist applied         // equally 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);         }     } }        
  • Attach SC_FPSController script to "FPSPlayer" Object (You volition notice that it likewise added another component called Character Controller, modify its center value to (0, 1, 0))
  • Assign the Primary Photographic camera to the Role player Photographic camera variable in SC_FPSController

The FPS controller is now ready!

Source: https://sharpcoderblog.com/blog/unity-3d-fps-controller

Posted by: coxouthad.blogspot.com

0 Response to "How To Make A Smooth First Person Camera In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel