Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- private CharacterController controller;
- private Vector3 playerVelocity;
- private bool IsGrounded;
- public float speed = 5f;
- public float gravity = -9.8f;
- public float jumpHeight = 3f;
- private bool lerpCrouch;
- private bool crouching;
- private bool isSprinting;
- private bool lockCursor = true;
- public float crouchTimer = 1f;
- public Camera myCam;
- public float walkFOV = 60f;
- public float runFOV = 120f;
- // Start is called before the first frame update
- void Start()
- {
- if (lockCursor)
- {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- controller = GetComponent<CharacterController>();
- myCam.fieldOfView = walkFOV;
- }
- // Update is called once per frame
- void Update()
- {
- IsGrounded = controller.isGrounded;
- if (lerpCrouch)
- {
- crouchTimer += Time.deltaTime;
- float p = crouchTimer / 1;
- p *= p;
- if (crouching)
- controller.height = Mathf.Lerp(controller.height, 1, p);
- else
- controller.height = Mathf.Lerp(controller.height, 2, p);
- if(p > 1)
- {
- lerpCrouch = false;
- crouchTimer = 0f;
- }
- }
- }
- // receive inputs for out inputmanager and apply them to the character controller
- public void ProcessMove(Vector2 input)
- {
- Vector3 moveDirection = Vector3.zero;
- moveDirection.x = input.x;
- moveDirection.z = input.y;
- controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
- playerVelocity.y += gravity * Time.deltaTime;
- if (IsGrounded && playerVelocity.y < 0)
- playerVelocity.y = -2f;
- controller.Move(playerVelocity * Time.deltaTime);
- //Debug.Log(playerVelocity.y);
- }
- public void Jump()
- {
- if (IsGrounded)
- {
- playerVelocity.y = Mathf.Sqrt(jumpHeight * -3.0f * gravity);
- }
- }
- public void Crouch()
- {
- crouching = !crouching;
- crouchTimer = 0;
- lerpCrouch = true;
- if (crouching)
- speed = 3;
- else
- speed = 5;
- }
- public void ToggleSprint()
- {
- isSprinting = !isSprinting;
- myCam.fieldOfView = isSprinting ? runFOV : walkFOV;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment