Advertisement
Guest User

Player movment

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