Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7. public float speed;
  8. public float jumpPower;
  9.  
  10. bool isGrounded;
  11. public float groundCheckLength;
  12. public LayerMask groundCheckLayer;
  13.  
  14. public float sensitivity;
  15. float playerRotation, cameraRotation;
  16. public Transform playerCam;
  17.  
  18. Rigidbody rb;
  19.  
  20. void Start () {
  21.  
  22. rb = GetComponent<Rigidbody>();
  23.  
  24. }
  25.  
  26. void Update () {
  27.  
  28. #region Movement
  29. float moveX = Input.GetAxis("Horizontal");
  30. float moveY = Input.GetAxis("Vertical");
  31.  
  32. Vector3 movement = (transform.forward * moveY) + (transform.right * moveX);
  33.  
  34. movement.x = Mathf.Clamp(movement.x, -1, 1);
  35. movement.z = Mathf.Clamp(movement.z, -1, 1);
  36.  
  37. rb.velocity = new Vector3(movement.x * speed, rb.velocity.y, movement.z * speed);
  38. #endregion
  39.  
  40. #region Camera
  41. float mouseX = Input.GetAxis("Mouse X");
  42. float mouseY = Input.GetAxis("Mouse Y");
  43.  
  44. playerRotation += mouseX * sensitivity;
  45. cameraRotation -= mouseY * sensitivity;
  46.  
  47. cameraRotation = Mathf.Clamp(cameraRotation, -50, 55);
  48.  
  49. transform.eulerAngles = new Vector3(transform.eulerAngles.x, playerRotation, transform.eulerAngles.z);
  50. playerCam.localEulerAngles = new Vector3(cameraRotation, 0, 0);
  51. #endregion
  52.  
  53. #region Jumping
  54. isGrounded = Physics.Raycast(transform.position, Vector3.up * -1, groundCheckLength, groundCheckLayer);
  55.  
  56. if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  57. {
  58. rb.AddForce(new Vector3(0, jumpPower, 0), ForceMode.VelocityChange);
  59. }
  60. #endregion
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement