Advertisement
Guest User

Jani´s Firsperson Control

a guest
Jan 17th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5.  
  6. public class Firstperson_Control : MonoBehaviour
  7. {
  8. Camera mainCamera;
  9.  
  10. [Header("Mouse")]
  11. public float mouseSensitivity = 100f;
  12. float mouseX, mouseY;
  13. float xRotation = 0f;
  14.  
  15. Rigidbody rb;
  16. CapsuleCollider cc;
  17. bool isGrounded;
  18.  
  19. bool cantStand;
  20. bool isCrouching;
  21. float orginalHeight;
  22. float wantedHeight;
  23.  
  24. [Header("Abilities")]
  25. public float gravity = 10.0f;
  26. public bool canMove;
  27.  
  28. float movingSpeed;
  29. float inputX, inputZ;
  30. Vector3 movingDirection;
  31.  
  32. public float walkSpeed = 3.2f;
  33. public float crouchSpeed = 2.4f;
  34. public float runSpeed = 4.6f;
  35.  
  36. public bool canJump;
  37. public float jumpForce;
  38.  
  39. [Header("Pick & Drop gameObject")]
  40. public float smooth;
  41. public float holdingdDistance;
  42. bool isCarrying;
  43.  
  44. //[HideInInspector]
  45. public GameObject carriedObject;
  46.  
  47. [Header("Cursor")]
  48. public Image cursor;
  49. public Sprite Default, Active;
  50.  
  51. private void Awake()
  52. {
  53. mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
  54.  
  55. if(mainCamera == null)
  56. {
  57. Debug.LogError("MainCamera tagged camera was not found");
  58. }
  59.  
  60. mainCamera.transform.position = transform.position + new Vector3(0, 1.5f, 0);
  61. mainCamera.transform.parent = transform;
  62.  
  63. rb = GetComponent<Rigidbody>();
  64.  
  65. if(rb == null)
  66. {
  67. rb = gameObject.AddComponent<Rigidbody>();
  68. }
  69.  
  70. rb.freezeRotation = true;
  71. rb.useGravity = false;
  72.  
  73. cc = GetComponent<CapsuleCollider>();
  74.  
  75. orginalHeight = cc.height;
  76. wantedHeight = orginalHeight / 2;
  77. }
  78.  
  79. private void Start()
  80. {
  81. Cursor.lockState = CursorLockMode.Locked;
  82. }
  83.  
  84. private void Update()
  85. {
  86. isGrounded = Physics.Raycast(transform.position + new Vector3(0,0.2f,0), -Vector3.up, 0.5f);
  87.  
  88. if (isCrouching)
  89. {
  90. cantStand = Physics.Raycast(transform.position + new Vector3(0, 1.6f, 0), Vector3.up, 0.5f);
  91. }
  92.  
  93. Inputs();
  94. Rotate();
  95. Speed();
  96. Crouching();
  97. }
  98.  
  99. private void FixedUpdate()
  100. {
  101. Move();
  102.  
  103. if(isCarrying)
  104. {
  105. Carry(carriedObject);
  106. }
  107.  
  108. if(!isCarrying)
  109. {
  110. PickUp();
  111. }
  112. }
  113.  
  114. void Inputs()
  115. {
  116. //Mouse
  117. mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  118. mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  119.  
  120. //Camera
  121. xRotation -= mouseY;
  122. xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  123.  
  124. if (isGrounded && canMove)
  125. {
  126. //Player
  127. inputX = Input.GetAxis("Horizontal");
  128. inputZ = Input.GetAxis("Vertical");
  129. }
  130.  
  131. //Calculate + Speed
  132. movingDirection = transform.right * inputX + transform.forward * inputZ;
  133. movingDirection = movingDirection.normalized * movingSpeed;
  134. }
  135.  
  136. void Rotate()
  137. {
  138. //Player
  139. transform.Rotate(Vector3.up * mouseX);
  140.  
  141. //Camera
  142. mainCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
  143. }
  144.  
  145. void Move()
  146. {
  147.  
  148. //Player
  149. Vector3 velocity = rb.velocity;
  150. Vector3 velocityChange = (movingDirection - velocity);
  151. velocityChange.y = 0;
  152. rb.AddForce(velocityChange, ForceMode.VelocityChange);
  153.  
  154. //Jump
  155. if (isGrounded && canJump && !isCrouching)
  156. {
  157. if (Input.GetKeyDown(KeyCode.Space))
  158. {
  159. rb.velocity = new Vector3(velocity.x, Mathf.Sqrt(2 * jumpForce * gravity), velocity.z);
  160. }
  161. }
  162.  
  163.  
  164. // We apply gravity manually for more tuning control
  165. rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));
  166. }
  167.  
  168. void Speed()
  169. {
  170. if(movingSpeed != crouchSpeed && isCrouching)
  171. {
  172. movingSpeed = crouchSpeed;
  173. }
  174.  
  175. if(movingSpeed != walkSpeed && !isCrouching)
  176. {
  177. movingSpeed = walkSpeed;
  178. }
  179.  
  180. if(movingSpeed != runSpeed && !isCrouching && Input.GetKey(KeyCode.LeftShift))
  181. {
  182. movingSpeed = runSpeed;
  183. }
  184. }
  185.  
  186. void Crouching()
  187. {
  188. if (Input.GetKeyDown(KeyCode.V) && !cantStand)
  189. {
  190. isCrouching = !isCrouching;
  191. }
  192.  
  193. if (!cantStand && !isCrouching)
  194. {
  195. cc.height = Mathf.Lerp(cc.height, orginalHeight, 5f * Time.deltaTime);
  196. cc.center = Vector3.Lerp(cc.center, new Vector3(0, orginalHeight / 2 , 0), 5f * Time.deltaTime);
  197. mainCamera.transform.position = Vector3.Slerp(mainCamera.transform.position, transform.position + new Vector3(0, 1.5f, 0), 5f * Time.deltaTime);
  198. }
  199.  
  200. if (!cantStand && isCrouching)
  201. {
  202. cc.height = Mathf.Lerp(cc.height, wantedHeight, 5f * Time.deltaTime);
  203. cc.center = Vector3.Lerp(cc.center, new Vector3(0, wantedHeight / 2, 0), 5f * Time.deltaTime);
  204. mainCamera.transform.position = Vector3.Slerp(mainCamera.transform.position, transform.position + new Vector3(0, 1f, 0), 5f * Time.deltaTime);
  205. }
  206. }
  207.  
  208. void Carry(GameObject o)
  209. {
  210. o.transform.position = Vector3.Lerp(o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * holdingdDistance, 5f * Time.deltaTime);
  211. }
  212.  
  213. void PickUp()
  214. {
  215. if(Input.GetKeyDown(KeyCode.F))
  216. {
  217. int x = Screen.width / 2;
  218. int y = Screen.height / 2;
  219.  
  220. Ray ray = mainCamera.ScreenPointToRay(new Vector3(x, y));
  221. RaycastHit hit;
  222. if(Physics.Raycast(ray, out hit))
  223. {
  224. if(hit.transform.tag == "Pickable")
  225. {
  226. carriedObject = hit.transform.gameObject;
  227. isCarrying = true;
  228. }
  229. }
  230. }
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement