Advertisement
Guest User

new movement

a guest
Jan 22nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3.  
  4. public class playerMovementScript : MonoBehaviour
  5. {
  6. public CharacterController controller;
  7. public float speed = 25f;
  8.  
  9. public float gravity = -9.81f;
  10. public Transform groundCheck;
  11. public float groundDistance = .4f;
  12. public float jumpHeight = 3f;
  13. public LayerMask groundMask;
  14.  
  15. bool isGrounded;
  16.  
  17. Vector3 velocity;
  18.  
  19. Camera cam;
  20. public Interaction focus;
  21.  
  22.  
  23. void Start()
  24. {
  25. cam = Camera.main;
  26. }
  27.  
  28.  
  29. void Update()
  30. {
  31. //is the player on the ground?
  32. isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  33. //if the player is grounded and has a y velocity less then 0 change it to 0 - -2
  34. if (isGrounded && velocity.y < 0 )
  35. {
  36. velocity.y = -2f;
  37. }
  38.  
  39. //controls movement on wasd
  40. float x = Input.GetAxis("Horizontal");
  41. float z = Input.GetAxis("Vertical");
  42.  
  43. Vector3 move = transform.right * x + transform.forward * z;
  44. controller.Move(move * speed * Time.deltaTime);
  45.  
  46. //jumping ability
  47. if (Input.GetButtonDown("Jump") && isGrounded)
  48. {
  49.  
  50. velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  51.  
  52. }
  53.  
  54. velocity.y += gravity * Time.deltaTime;
  55. controller.Move(velocity * Time.deltaTime);
  56.  
  57.  
  58.  
  59. //mouse buttons controls
  60.  
  61. if (Input.GetMouseButtonDown(0))
  62. {
  63. //creates an invisble ray from the player to the object being clicked on
  64. Ray ray = cam.ScreenPointToRay(Input.mousePosition);
  65. RaycastHit hit;
  66.  
  67.  
  68. //if the ray hits
  69. if (Physics.Raycast(ray, out hit, 100))
  70. {
  71. Interaction Interaction = hit.collider.GetComponent<Interaction>();
  72. if (Interaction != null)
  73. {
  74. removeFocus(Interaction);
  75. }
  76. }
  77. }
  78.  
  79.  
  80. //Right mouse button code
  81. if (Input.GetMouseButtonDown(1))
  82. {
  83. //creates an invisble ray from the player to the object being clicked on
  84. Ray ray = cam.ScreenPointToRay(Input.mousePosition);
  85. RaycastHit hit;
  86.  
  87.  
  88. //if the ray hits
  89. if (Physics.Raycast(ray, out hit, 100))
  90. {
  91. Interaction Interaction = hit.collider.GetComponent<Interaction>();
  92. if (Interaction != null)
  93. {
  94. setFocus(Interaction);
  95. }
  96. //if we do hit an interactable
  97.  
  98. }
  99.  
  100.  
  101. }
  102.  
  103. }
  104. void setFocus(Interaction newFocus)
  105. {
  106. if (newFocus != focus)
  107. {
  108. if (focus != null)
  109. focus.onDefocused();
  110.  
  111. focus = newFocus;
  112.  
  113. }
  114.  
  115. newFocus.onFocused(transform);
  116.  
  117. }
  118.  
  119. void removeFocus(Interaction newFocus)
  120. {
  121. if (focus != null)
  122. focus.onDefocused();
  123.  
  124. focus = null;
  125.  
  126. }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement