First Person Controller - Zoom/ADS (EP07) [Unity Tutorial]

  Рет қаралды 11,906

Comp-3 Interactive

Comp-3 Interactive

Күн бұрын

Пікірлер: 24
@trblemayker5157
@trblemayker5157 3 жыл бұрын
6:22 LOL Great tutorial series so far. Very well explained and the structured coding makes it easy to follow. It'd be neat if you could show us how to use the mouse wheel to zoom in increments or slowdown/speed up movement like the old Splinter Cell games.
@rooftoprumble7845
@rooftoprumble7845 2 жыл бұрын
You're honestly an absolute GOD, I'm studying Game Design at University and I feel like I learned more about Unity and scripting from you than at Uni. Also the way you strucure and explain your code is just G.O.A.T like behaviour lol
@washynator
@washynator 3 жыл бұрын
Hahaha, that Methf really cracked me up :D You're awesome!
@paulberry3359
@paulberry3359 3 жыл бұрын
This series just gets better and better! So excited to see where this completed FPS Controller leads to
@KillerKaneoss
@KillerKaneoss Жыл бұрын
thanks alot, with this ive made a Zoom Headbob, and sliding if you press C on your keyboard. i love it
@NhanThanh-ib9wb
@NhanThanh-ib9wb 3 жыл бұрын
I haven't seen it yet as well as this series. But I will, I think yours going to be good series. Thank you! I wish you success.
@dremovremen1764
@dremovremen1764 Жыл бұрын
If you've just copied zoomKey line like I did, zoom might not work for you because Unity automatically "captured" zoomKey as LeftControl. Be sure to replace it with Mouse1 in the Unity window itself.
@Golemofstone
@Golemofstone 3 жыл бұрын
Great tutorials, almost as great as your "Salty sea dog accent" in your game jam entery (Which was very good imho)
@nutsandy7183
@nutsandy7183 3 жыл бұрын
Loving the tuts, keep em coming please :)
@Sergey_gameDev
@Sergey_gameDev 5 ай бұрын
mr BEEEEEEEEEEEEEEEEST!
@AquilaPebble
@AquilaPebble Жыл бұрын
How would you implement this into the sprinting
@Ophelos
@Ophelos 3 жыл бұрын
Just remember you can use this same code for making a "Sniper scope".
@DonWon-uz3wp
@DonWon-uz3wp 5 ай бұрын
Where can I find the Meth f tutorial?
@koniipinka7923
@koniipinka7923 3 жыл бұрын
Yes!
@Maarten_Hofmans
@Maarten_Hofmans 3 жыл бұрын
Can you add a door controller? So when the players comes close to the door there appear a UI that says "press E to interact" to open a door and close a door with sn animation.
@MattyZ14
@MattyZ14 2 жыл бұрын
I followed this tutorial and everything works except the zoom out. I can zoom in when I push the button down, but then when I release the button it's not zooming out. Any idea why?
@koniipinka7923
@koniipinka7923 3 жыл бұрын
Awesome
@tomkiptom
@tomkiptom 3 жыл бұрын
hhahahah ending was funny :P
@pedropc5824
@pedropc5824 3 жыл бұрын
Yeah lol
@milosauvage2893
@milosauvage2893 Жыл бұрын
Ou est le code ?
@eclipse_ai
@eclipse_ai 2 жыл бұрын
I had this same issue with headbob that I ended up disabling cause I could not locate the issue, but its affecting zoom to. When I use it, it happens way to fast or just not at all, I at first figured maybe I typed out a number incorrectly but have spent hours re reading and making sure. my current theory is its a computer thing as ive head that some computers can cause unity scripts to not load properly If anyone could help thatd be great, ill put the code itself in a reply of this.
@eclipse_ai
@eclipse_ai 2 жыл бұрын
using System.Collections; using System.Collections.Generic; using UnityEngine; public class FirstPersonController : MonoBehaviour { public bool CanMove { get; private set; } = true; private bool IsSprinting => canSprint && Input.GetKey(sprintKey); private bool ShouldJump => Input.GetKeyDown(jumpKey) && characterController.isGrounded; private bool ShouldCrouch => Input.GetKeyDown(crouchKey) && !duringCrouchAnimation && characterController.isGrounded; [Header("Functional Options")] [SerializeField] private bool canSprint = true; [SerializeField] private bool canJump = true; [SerializeField] private bool canCrouch = true; [SerializeField] private bool canUseHeadBob = true; [SerializeField] private bool willSlideOnSlopes = true; [SerializeField] private bool canZoom = true; [Header("Controls")] [SerializeField] private KeyCode sprintKey = KeyCode.LeftShift; [SerializeField] private KeyCode jumpKey = KeyCode.Space; [SerializeField] private KeyCode crouchKey = KeyCode.LeftControl; [SerializeField] private KeyCode zoomKey = KeyCode.Mouse1; [Header("Movement Parameters")] [SerializeField] private float walkSpeed = 3.0f; [SerializeField] private float sprintSpeed = 8.0f; [SerializeField] private float crouchSpeed = 1.5f; [SerializeField] private float slopeSpeed = 8f; [Header("Look Parameters")] [SerializeField, Range(1, 10)] private float lookSpeedX = 2.0f; [SerializeField, Range(1, 10)] private float lookSpeedY = 2.0f; [SerializeField, Range(1, 180)] private float upperLookLimit = 80.0f; [SerializeField, Range(1, 180)] private float lowerLookLimit = 80.0f; [Header("Jumping Parameters")] [SerializeField] private float jumpForce = 8.0f; [SerializeField] private float gravity = 30.0f; [Header("Crouch Parameters")] [SerializeField] private float crouchHeight = 0.5f; [SerializeField] private float standHeight = 2f; [SerializeField] private float timeToCrouch = 0.25f; [SerializeField] private Vector3 crouchingCenter = new Vector3(0, 0.5f, 0); [SerializeField] private Vector3 standingCenter = new Vector3(0, 0, 0); private bool isCrouching; private bool duringCrouchAnimation; [Header("Headbob Parameters")] [SerializeField] private float walkBobSpeed = 14f; [SerializeField] private float walkBobAmount = 0.05f; [SerializeField] private float sprintBobSpeed = 18f; [SerializeField] private float sprintBobAmount = 0.1f; [SerializeField] private float crouchBobSpeed = 8f; [SerializeField] private float crouchBobAmount = 0.025f; private float defaultYPos = 0; private float timer; [Header("Zoom Parameters")] [SerializeField] private float timeToZoom = 0.3f; [SerializeField] private float zoomFOV = 30f; private float defaultFOV; private Coroutine zoomRoutine; private Vector3 hitPointNormal; private bool IsSlideing { get { if(characterController.isGrounded && Physics.Raycast(transform.position, Vector3.down, out RaycastHit slopeHit, 2.1f)) { hitPointNormal = slopeHit.normal; return Vector3.Angle(hitPointNormal, Vector3.up) > characterController.slopeLimit; } else { return false; } } } private Camera playerCamera; private CharacterController characterController; private Vector3 moveDirection; private Vector2 currentInput; private float rotationX = 0; // Start is called before the first frame update void Awake() { playerCamera = GetComponentInChildren(); characterController = GetComponent(); defaultYPos = playerCamera.transform.localPosition.y; defaultFOV = playerCamera.fieldOfView; Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } // Update is called once per frame void Update() { if (CanMove) { HandleMovementInput(); HandleMouseLook(); if (canJump) HandleJump(); if (canCrouch) HandleCrouch(); if (canUseHeadBob) HandleHeadBob(); if (canZoom) HandleZoom(); ApplyFinalMovements(); } } private void HandleMovementInput() { currentInput = new Vector2((isCrouching ? crouchSpeed : IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Vertical"), (isCrouching ? crouchSpeed : IsSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Horizontal")); float moveDirectionY = moveDirection.y; moveDirection = (transform.TransformDirection(Vector3.forward) * currentInput.x) + (transform.TransformDirection(Vector3.right) * currentInput.y); moveDirection.y = moveDirectionY; } private void HandleMouseLook() { rotationX -= Input.GetAxis("Mouse Y") * lookSpeedY; rotationX = Mathf.Clamp(rotationX, -upperLookLimit, lowerLookLimit); playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0); transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeedX, 0); } private void HandleZoom() { if(Input.GetKeyDown(zoomKey)) if(zoomRoutine != null) { StopCoroutine(zoomRoutine); zoomRoutine = null; } zoomRoutine = StartCoroutine(ToggleZoom(true)); if (Input.GetKeyUp(zoomKey)) if (zoomRoutine != null) { StopCoroutine(zoomRoutine); zoomRoutine = null; } zoomRoutine = StartCoroutine(ToggleZoom(false)); } private void HandleJump() { if (ShouldJump) moveDirection.y = jumpForce; } private void HandleCrouch() { if (ShouldCrouch) StartCoroutine(CrouchStand()); } private void HandleHeadBob() { if (!characterController.isGrounded) return; if(Mathf.Abs(moveDirection.x) > 0.1f || Mathf.Abs(moveDirection.z) > 0.1f) { timer = +Time.deltaTime * (isCrouching ? crouchBobSpeed : IsSprinting ? sprintBobSpeed : walkBobSpeed); playerCamera.transform.localPosition = new Vector3( playerCamera.transform.localPosition.x, defaultYPos + Mathf.Sin(timer ) * (isCrouching ? crouchBobAmount : IsSprinting ? sprintBobAmount : walkBobAmount), playerCamera.transform.localPosition.z); } } private void ApplyFinalMovements() { if (!characterController.isGrounded) moveDirection.y -= gravity * Time.deltaTime; if (willSlideOnSlopes && IsSlideing) moveDirection += new Vector3(hitPointNormal.x, -hitPointNormal.y, hitPointNormal.z) * slopeSpeed; characterController.Move(moveDirection * Time.deltaTime); } private IEnumerator CrouchStand() { if (isCrouching && Physics.Raycast(playerCamera.transform.position, Vector3.up, 1f)) yield break; duringCrouchAnimation = true; float timeElapsed = 0; float targetHeight = isCrouching ? standHeight : crouchHeight; float currentHeight = characterController.height; Vector3 targetCenter = isCrouching ? standingCenter : crouchingCenter; Vector3 currentCenter = characterController.center; while(timeElapsed < timeToCrouch) { characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed/timeToCrouch); characterController.center = Vector3.Lerp(currentCenter, targetCenter, timeElapsed / timeToCrouch); timeElapsed += Time.deltaTime; yield return null; } characterController.height = targetHeight; characterController.center = targetCenter; isCrouching = !isCrouching; duringCrouchAnimation = false; } private IEnumerator ToggleZoom(bool isEnter) { float targetFOV = isEnter ? zoomFOV : defaultFOV; float startingFOV = playerCamera.fieldOfView; float timeElapsed = 0; while(timeElapsed < timeToZoom) { playerCamera.fieldOfView = Mathf.Lerp(startingFOV, targetFOV, timeElapsed / timeToZoom); timeElapsed += Time.deltaTime; yield return null; } playerCamera.fieldOfView = targetFOV; zoomRoutine = null; } }
@koniipinka7923
@koniipinka7923 3 жыл бұрын
Me waiting for footstep sounds and pause menu
@koniipinka7923
@koniipinka7923 3 жыл бұрын
Me waiting for footstep sounds and pause menu
First Person Controller - Interaction (EP08) [Unity Tutorial]
16:33
Comp-3 Interactive
Рет қаралды 20 М.
First Person Controller - Crouching (EP04) [Unity Tutorial]
16:40
Comp-3 Interactive
Рет қаралды 33 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 41 МЛН
THIRD PERSON MOVEMENT in Unity
21:05
Brackeys
Рет қаралды 1,5 МЛН
First Person Controller - Footsteps (EP09) [Unity Tutorial]
12:36
Comp-3 Interactive
Рет қаралды 30 М.
I Made the Same Game in 8 Engines
12:34
Emeral
Рет қаралды 4,3 МЛН
Creating a STAMINA SYSTEM in Unity | Unity UI
20:32
SpeedTutor
Рет қаралды 31 М.
ADVANCED 3D DASH ABILITY in 11 MINUTES - Unity Tutorial
11:17
Dave / GameDevelopment
Рет қаралды 67 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН