GamerRO

Untitled

Aug 19th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.Utility;
  5. using Steamworks;
  6. using UnityStandardAssets.CrossPlatformInput;
  7.  
  8. [RequireComponent(typeof(PlayerMotor))]
  9. [RequireComponent(typeof(CharacterController))]
  10. public class PlayerController : MonoBehaviour
  11. {
  12.  
  13. [SerializeField] private CharacterController m_CharacterController;
  14.  
  15. public bool canJump;
  16.  
  17. [SerializeField]
  18. private float Walkspeed = 5f;
  19.  
  20. [SerializeField]
  21. private float Runspeed = 8f;
  22.  
  23. [SerializeField] private float JumpSpeed = 6.6f;
  24.  
  25. [SerializeField]
  26. private float lookSensitivity = 3f;
  27.  
  28. private PlayerMotor motor;
  29.  
  30. private Camera m_Camera;
  31.  
  32.  
  33. void Start()
  34. {
  35. motor = GetComponent<PlayerMotor>();
  36. m_Camera = Camera.main;
  37. }
  38.  
  39. void Update()
  40. {
  41. if (PauseMenu.IsOn)
  42. {
  43. if (Cursor.lockState != CursorLockMode.None)
  44. Cursor.lockState = CursorLockMode.None;
  45.  
  46. motor.Move(Vector3.zero);
  47. motor.Rotate(Vector3.zero);
  48. motor.RotateCamera(0f);
  49.  
  50. return;
  51. }
  52.  
  53. if (Cursor.lockState != CursorLockMode.Locked)
  54. {
  55. Cursor.lockState = CursorLockMode.Locked;
  56. }
  57.  
  58. //Calculate movement velocity as a 3D vector
  59. float _xMov = Input.GetAxis("Horizontal");
  60. float _zMov = Input.GetAxis("Vertical");
  61.  
  62. Vector3 _movHorizontal = transform.right * _xMov;
  63. Vector3 _movVertical = transform.forward * _zMov;
  64.  
  65. if (Input.GetKey(KeyCode.LeftShift))
  66. {
  67. // Final movement vector
  68. Vector3 _velocity = (_movHorizontal + _movVertical) * Runspeed;
  69. //Apply movement
  70. motor.Move(_velocity);
  71. }
  72. else
  73. {
  74. // Final movement vector
  75. Vector3 _velocity = (_movHorizontal + _movVertical) * Walkspeed;
  76. //Apply movement
  77. motor.Move(_velocity);
  78. }
  79.  
  80. //Calculate rotation as a 3D vector (turning around)
  81. float _yRot = Input.GetAxisRaw("Mouse X");
  82.  
  83. Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity * Time.deltaTime * 30f;
  84.  
  85. //Apply rotation
  86. motor.Rotate(_rotation);
  87.  
  88. //Calculate camera rotation as a 3D vector (turning around)
  89. float _xRot = Input.GetAxisRaw("Mouse Y");
  90.  
  91. float _cameraRotationX = _xRot * lookSensitivity * Time.deltaTime * 30f;
  92.  
  93. //Apply camera rotation
  94. motor.RotateCamera(_cameraRotationX);
  95.  
  96. //Get if player pressed Space and Jump
  97. if (Input.GetButtonDown("Jump") && canJump == true)
  98. {
  99. Vector3 jumpvel = new Vector3(0f, JumpSpeed, 0f);
  100.  
  101. motor.Jump(jumpvel);
  102.  
  103. motor.PlayJumpSound();
  104.  
  105. canJump = false;
  106. }
  107. }
  108.  
  109. private void OnCollisionEnter(Collision collision)
  110. {
  111. if (collision.transform.name == "Floor" || collision.transform.name == "Table")
  112. {
  113. Vector3 jumpvel = new Vector3(0f, 0f, 0f);
  114. motor.Jump(jumpvel);
  115. motor.PlayLandingSound();
  116. canJump = true;
  117. }
  118. }
  119.  
  120. }
Add Comment
Please, Sign In to add comment