Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class playerController : MonoBehaviour
  6. {
  7. [SerializeField] private string mouseXName, mouseYName;
  8. [SerializeField] private float mouseSens;
  9. [SerializeField] private string horizontalInputName;
  10. [SerializeField] private string verticalInputName;
  11. [SerializeField] private Transform playerTransform;
  12. [SerializeField] private float moveSpeed;
  13.  
  14.  
  15. private CharacterController charCtrl;
  16. private Transform playerCamera;
  17. private float xAxisClamp;
  18. private Vector3 moveDir;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22.  
  23. }
  24.  
  25. private void Awake(){
  26. lockCursor();
  27. charCtrl = transform.GetComponent<CharacterController>();
  28. playerCamera = transform.Find("Main Camera");
  29. xAxisClamp = 0.0f;
  30. }
  31.  
  32. private void lockCursor(){
  33. Cursor.lockState = CursorLockMode.Locked;
  34. }
  35.  
  36. private void cameraRotation(){
  37. float mouseX = Input.GetAxis(mouseXName) * mouseSens * Time.deltaTime;
  38. float mouseY = Input.GetAxis(mouseYName) * mouseSens * Time.deltaTime;
  39.  
  40. xAxisClamp += mouseY;
  41.  
  42. if(xAxisClamp > 90f){
  43. xAxisClamp = 90.0f;
  44. mouseY = 0.0f;
  45. clampXAxisRotationToValue(270.0f);
  46. }else if(xAxisClamp < -90f){
  47. xAxisClamp = -90.0f;
  48. mouseY = 0.0f;
  49. clampXAxisRotationToValue(-270.0f);
  50. }
  51.  
  52. playerCamera.Rotate(Vector3.left * mouseY);
  53.  
  54. playerTransform.Rotate(Vector3.up * mouseX);
  55. }
  56.  
  57. private void clampXAxisRotationToValue(float value){
  58. Vector3 eulerRotation = playerCamera.eulerAngles;
  59. eulerRotation.x = value;
  60. playerCamera.eulerAngles = eulerRotation;
  61. }
  62.  
  63. private void playerMovement(){
  64. float horizInput = Input.GetAxis(horizontalInputName) * moveSpeed * Time.deltaTime;
  65. float vertInput = Input.GetAxis(verticalInputName) * moveSpeed * Time.deltaTime;
  66.  
  67. Vector3 forwardMovement = playerCamera.forward * vertInput;
  68. Vector3 rightMovement = playerCamera.right * horizInput;
  69.  
  70. //Simpemove? vs move()?
  71. charCtrl.SimpleMove(forwardMovement + rightMovement);
  72. }
  73.  
  74.  
  75. // Update is called once per frame
  76. void Update()
  77. {
  78. playerMovement();
  79. cameraRotation();
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement