Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using UnityEngine;
- public class Firstperson_Control : MonoBehaviour
- {
- Camera mainCamera;
- [Header("Mouse")]
- public float mouseSensitivity = 100f;
- float mouseX, mouseY;
- float xRotation = 0f;
- Rigidbody rb;
- CapsuleCollider cc;
- bool isGrounded;
- bool cantStand;
- bool isCrouching;
- float orginalHeight;
- float wantedHeight;
- [Header("Abilities")]
- public float gravity = 10.0f;
- public bool canMove;
- float movingSpeed;
- float inputX, inputZ;
- Vector3 movingDirection;
- public float walkSpeed = 3.2f;
- public float crouchSpeed = 2.4f;
- public float runSpeed = 4.6f;
- public bool canJump;
- public float jumpForce;
- [Header("Pick & Drop gameObject")]
- public float smooth;
- public float holdingdDistance;
- bool isCarrying;
- //[HideInInspector]
- public GameObject carriedObject;
- [Header("Cursor")]
- public Image cursor;
- public Sprite Default, Active;
- private void Awake()
- {
- mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
- if(mainCamera == null)
- {
- Debug.LogError("MainCamera tagged camera was not found");
- }
- mainCamera.transform.position = transform.position + new Vector3(0, 1.5f, 0);
- mainCamera.transform.parent = transform;
- rb = GetComponent<Rigidbody>();
- if(rb == null)
- {
- rb = gameObject.AddComponent<Rigidbody>();
- }
- rb.freezeRotation = true;
- rb.useGravity = false;
- cc = GetComponent<CapsuleCollider>();
- orginalHeight = cc.height;
- wantedHeight = orginalHeight / 2;
- }
- private void Start()
- {
- Cursor.lockState = CursorLockMode.Locked;
- }
- private void Update()
- {
- isGrounded = Physics.Raycast(transform.position + new Vector3(0,0.2f,0), -Vector3.up, 0.5f);
- if (isCrouching)
- {
- cantStand = Physics.Raycast(transform.position + new Vector3(0, 1.6f, 0), Vector3.up, 0.5f);
- }
- Inputs();
- Rotate();
- Speed();
- Crouching();
- }
- private void FixedUpdate()
- {
- Move();
- if(isCarrying)
- {
- Carry(carriedObject);
- }
- if(!isCarrying)
- {
- PickUp();
- }
- }
- void Inputs()
- {
- //Mouse
- mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
- mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
- //Camera
- xRotation -= mouseY;
- xRotation = Mathf.Clamp(xRotation, -90f, 90f);
- if (isGrounded && canMove)
- {
- //Player
- inputX = Input.GetAxis("Horizontal");
- inputZ = Input.GetAxis("Vertical");
- }
- //Calculate + Speed
- movingDirection = transform.right * inputX + transform.forward * inputZ;
- movingDirection = movingDirection.normalized * movingSpeed;
- }
- void Rotate()
- {
- //Player
- transform.Rotate(Vector3.up * mouseX);
- //Camera
- mainCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
- }
- void Move()
- {
- //Player
- Vector3 velocity = rb.velocity;
- Vector3 velocityChange = (movingDirection - velocity);
- velocityChange.y = 0;
- rb.AddForce(velocityChange, ForceMode.VelocityChange);
- //Jump
- if (isGrounded && canJump && !isCrouching)
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- rb.velocity = new Vector3(velocity.x, Mathf.Sqrt(2 * jumpForce * gravity), velocity.z);
- }
- }
- // We apply gravity manually for more tuning control
- rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));
- }
- void Speed()
- {
- if(movingSpeed != crouchSpeed && isCrouching)
- {
- movingSpeed = crouchSpeed;
- }
- if(movingSpeed != walkSpeed && !isCrouching)
- {
- movingSpeed = walkSpeed;
- }
- if(movingSpeed != runSpeed && !isCrouching && Input.GetKey(KeyCode.LeftShift))
- {
- movingSpeed = runSpeed;
- }
- }
- void Crouching()
- {
- if (Input.GetKeyDown(KeyCode.V) && !cantStand)
- {
- isCrouching = !isCrouching;
- }
- if (!cantStand && !isCrouching)
- {
- cc.height = Mathf.Lerp(cc.height, orginalHeight, 5f * Time.deltaTime);
- cc.center = Vector3.Lerp(cc.center, new Vector3(0, orginalHeight / 2 , 0), 5f * Time.deltaTime);
- mainCamera.transform.position = Vector3.Slerp(mainCamera.transform.position, transform.position + new Vector3(0, 1.5f, 0), 5f * Time.deltaTime);
- }
- if (!cantStand && isCrouching)
- {
- cc.height = Mathf.Lerp(cc.height, wantedHeight, 5f * Time.deltaTime);
- cc.center = Vector3.Lerp(cc.center, new Vector3(0, wantedHeight / 2, 0), 5f * Time.deltaTime);
- mainCamera.transform.position = Vector3.Slerp(mainCamera.transform.position, transform.position + new Vector3(0, 1f, 0), 5f * Time.deltaTime);
- }
- }
- void Carry(GameObject o)
- {
- o.transform.position = Vector3.Lerp(o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * holdingdDistance, 5f * Time.deltaTime);
- }
- void PickUp()
- {
- if(Input.GetKeyDown(KeyCode.F))
- {
- int x = Screen.width / 2;
- int y = Screen.height / 2;
- Ray ray = mainCamera.ScreenPointToRay(new Vector3(x, y));
- RaycastHit hit;
- if(Physics.Raycast(ray, out hit))
- {
- if(hit.transform.tag == "Pickable")
- {
- carriedObject = hit.transform.gameObject;
- isCarrying = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement