Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.AI;
- public class playerMovementScript : MonoBehaviour
- {
- public CharacterController controller;
- public float speed = 25f;
- public float gravity = -9.81f;
- public Transform groundCheck;
- public float groundDistance = .4f;
- public float jumpHeight = 3f;
- public LayerMask groundMask;
- bool isGrounded;
- Vector3 velocity;
- Camera cam;
- public Interaction focus;
- void Start()
- {
- cam = Camera.main;
- }
- void Update()
- {
- //is the player on the ground?
- isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- //if the player is grounded and has a y velocity less then 0 change it to 0 - -2
- if (isGrounded && velocity.y < 0 )
- {
- velocity.y = -2f;
- }
- //controls movement on wasd
- float x = Input.GetAxis("Horizontal");
- float z = Input.GetAxis("Vertical");
- Vector3 move = transform.right * x + transform.forward * z;
- controller.Move(move * speed * Time.deltaTime);
- //jumping ability
- if (Input.GetButtonDown("Jump") && isGrounded)
- {
- velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
- }
- velocity.y += gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime);
- //mouse buttons controls
- if (Input.GetMouseButtonDown(0))
- {
- //creates an invisble ray from the player to the object being clicked on
- Ray ray = cam.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- //if the ray hits
- if (Physics.Raycast(ray, out hit, 100))
- {
- Interaction Interaction = hit.collider.GetComponent<Interaction>();
- if (Interaction != null)
- {
- removeFocus(Interaction);
- }
- }
- }
- //Right mouse button code
- if (Input.GetMouseButtonDown(1))
- {
- //creates an invisble ray from the player to the object being clicked on
- Ray ray = cam.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- //if the ray hits
- if (Physics.Raycast(ray, out hit, 100))
- {
- Interaction Interaction = hit.collider.GetComponent<Interaction>();
- if (Interaction != null)
- {
- setFocus(Interaction);
- }
- //if we do hit an interactable
- }
- }
- }
- void setFocus(Interaction newFocus)
- {
- if (newFocus != focus)
- {
- if (focus != null)
- focus.onDefocused();
- focus = newFocus;
- }
- newFocus.onFocused(transform);
- }
- void removeFocus(Interaction newFocus)
- {
- if (focus != null)
- focus.onDefocused();
- focus = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement