-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstPersonController.cs
157 lines (133 loc) · 4.5 KB
/
FirstPersonController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Hyperbeam;
using System;
using System.Collections;
using System.Collections.Generic;
using DefaultNamespace;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.UI;
public class FirstPersonController : MonoBehaviour
{
[SerializeField]
public GameObject PlayerCam;
public GameObject BrowserImage;
public CharacterController CharacterController;
public PlayerInput playerControl;
public HyperbeamController hb;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float mouseSensitivity = 20;
public float movementSpeed = 5;
public float gravity = -9.81f;
public float interactionDistance = 0.5f;
public Transform interactionCenter;
public WritablePanel interactionPrompt;
private float xRotation = 0f;
private Vector2 inputVec = Vector2.zero;
private Vector3 _velocity;
private bool _isGrounded;
private bool _isUIOpen;
private Collider[] _interactables = new Collider[1];
private Interactable _currentInteractable = null;
// Start is called before the first frame update
void Start()
{
if (CharacterController == null)
{
CharacterController = gameObject.GetComponent<CharacterController>();
}
playerControl = gameObject.GetComponent<PlayerInput>();
hb.OnControlReturned.AddListener(ControlReturned);
}
public void ControlReturned()
{
Cursor.lockState = CursorLockMode.Locked;
playerControl.SwitchCurrentActionMap("Player");
BrowserImage.SetActive(false);
_isUIOpen = false;
}
// Update is called once per frame
void Update()
{
var moveVec = Vector3.zero;
_isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (inputVec != Vector2.zero)
{
moveVec = transform.right * inputVec.x + transform.forward * inputVec.y;
}
if (_isGrounded && _velocity.y < 0)
{
_velocity.y = -1f;
}
else
{
_velocity.y += gravity * Time.deltaTime;
}
CharacterController.Move(((moveVec * movementSpeed) + _velocity) * Time.deltaTime);
}
private void checkForInteractables()
{
_interactables[0] = null;
Physics.OverlapSphereNonAlloc(interactionCenter.position, interactionDistance, _interactables, LayerMask.GetMask("Interactable"));
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(interactionCenter.position, interactionDistance);
}
public void OnInteract()
{
Debug.Log("On Interact called");
if (_currentInteractable != null)
{
Debug.Log($"Attempting to interact with: {_currentInteractable.gameObject}");
_currentInteractable.Interact();
interactionPrompt.gameObject.SetActive(false);
}
}
public void OnMove(InputAction.CallbackContext ctx)
{
inputVec = ctx.ReadValue<Vector2>();
checkForInteractables();
if (_interactables[0] != null)
{
var interactable = _interactables[0].GetComponent<Interactable>();
interactionPrompt.gameObject.SetActive(true);
interactionPrompt.SetPrompt(interactable.interactionInfo);
_currentInteractable = interactable;
}
else
{
interactionPrompt.gameObject.SetActive(false);
}
}
public void OnLook(InputAction.CallbackContext ctx)
{
if (Cursor.lockState != CursorLockMode.Locked)
return;
var lookInput = ctx.ReadValue<Vector2>();
xRotation -= lookInput.y * mouseSensitivity;
xRotation = Mathf.Clamp(xRotation, -80f, 90f);
PlayerCam.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
gameObject.transform.Rotate(Vector3.up, lookInput.x * mouseSensitivity);
}
public void OnFire()
{
Cursor.lockState = CursorLockMode.Locked;
}
public void OnReleaseLock()
{
Cursor.lockState = CursorLockMode.None;
}
public void OnOpenUI()
{
if (hb.Instance == null) return;
if (_isUIOpen) return;
Cursor.lockState = CursorLockMode.None;
playerControl.SwitchCurrentActionMap("UI");
BrowserImage.SetActive(true);
hb.PassControlToBrowser("q", false, false, true, false);
_isUIOpen = true;
}
}