Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerScript : MonoBehaviour {
- //Movement
- CharacterController player;
- public GameObject FPSCam;
- public float Sensitivity = 5f;
- [SerializeField]
- private float SpeedFB = 350;
- [SerializeField]
- private float SpeedLR = 250;
- private bool hasJumped, isCrouched, isSprinting;
- private float moveFB, moveLR, rotX, rotY, verticalVelocity, jumpForce = 5f;
- void Start ()
- {
- player = this.GetComponent<CharacterController>();
- Cursor.lockState = CursorLockMode.Locked;
- SpeedFB = 350;
- SpeedLR = 250;
- }
- private void Update ()
- {
- Movement();
- Sprint();
- Jump();
- Crouch();
- ApplyGravity();
- }
- private void Movement ()
- {
- moveFB = Input.GetAxis("Vertical") * SpeedFB * Time.deltaTime;
- moveLR = Input.GetAxis("Horizontal") * SpeedLR * Time.deltaTime;
- rotX = Input.GetAxis("Mouse X") * Sensitivity * Time.deltaTime;
- rotY = Input.GetAxis("Mouse Y") * Sensitivity * Time.deltaTime;
- Vector3 movement = new Vector3(moveLR, verticalVelocity, moveFB);
- movement = transform.rotation * movement;
- transform.Rotate(0, rotX, 0);
- FPSCam.transform.Rotate(-rotY, 0, 0);
- player.Move(movement * Time.deltaTime);
- }
- private void Sprint()
- {
- if (Input.GetButtonDown("Sprint"))
- {
- if (isCrouched)
- {
- player.height = 2f;
- isCrouched = false;
- SpeedFB = 350;
- SpeedLR = 250;
- }
- if(isSprinting == false)
- {
- isSprinting = true;
- player.height = 2f;
- SpeedFB = SpeedFB * 1.5f;
- SpeedLR = SpeedLR * 1.25f;
- }
- }
- if (Input.GetButtonUp("Sprint"))
- {
- if (isCrouched)
- {
- isSprinting = false;
- SpeedFB = 175f;
- SpeedLR = 125f;
- }
- else
- {
- isSprinting = false;
- SpeedFB = 350f;
- SpeedLR = 250f;
- }
- }
- }
- private void Crouch()
- {
- if (Input.GetButtonDown("Crouch"))
- {
- if(isCrouched == false)
- {
- isCrouched = true;
- isSprinting = false;
- player.height = player.height / 2;
- SpeedFB = 175;
- SpeedLR = 125;
- }
- }
- if (Input.GetButtonUp("Crouch"))
- {
- if (isSprinting)
- {
- player.height = 2f;
- isCrouched = false;
- }
- else
- {
- player.height = 2f;
- isCrouched = false;
- SpeedFB = 350;
- SpeedLR = 250;
- }
- }
- }
- private void Jump()
- {
- if (Input.GetButtonDown("Jump"))
- {
- hasJumped = true;
- }
- }
- private void ApplyGravity()
- {
- if (player.isGrounded)
- {
- if(hasJumped == false)
- {
- verticalVelocity = Physics.gravity.y;
- }
- else
- {
- verticalVelocity = jumpForce;
- }
- }
- else
- {
- verticalVelocity += Physics.gravity.y * Time.deltaTime;
- verticalVelocity = Mathf.Clamp(verticalVelocity, -12f, jumpForce);
- hasJumped = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement