This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSpectatorFlycam.cs
64 lines (57 loc) · 1.97 KB
/
SpectatorFlycam.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
using Assets.Gamelogic.Utils;
using Improbable;
using Improbable.General;
using Improbable.Unity.Visualizer;
using UnityEngine;
namespace Assets.Gamelogic.Player
{
public class SpectatorFlycam : MonoBehaviour
{
[Require] private Position.Writer PositionWriter;
[Require] private Rotation.Writer RotationWriter;
private float yaw;
private float pitch;
private const float movementSpeed = 10.0f;
[SerializeField] private GameObject SpectatorCamera;
void OnEnable()
{
Cursor.lockState = CursorLockMode.Locked;
SpectatorCamera.SetActive(true);
}
void Update()
{
HandleRotationMovement();
HandlePositionMovement();
var rot = transform.rotation;
PositionWriter.Send(new Position.Update().SetCoords(transform.position.ToCoordinates()));
RotationWriter.Send(new Rotation.Update().SetRotation(new Improbable.Global.Quaternion(rot.x, rot.y, rot.z, rot.w)));
}
private void HandlePositionMovement()
{
Vector3 targetDirection = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
targetDirection += transform.forward;
}
if (Input.GetKey(KeyCode.A))
{
targetDirection -= transform.right;
}
if (Input.GetKey(KeyCode.S))
{
targetDirection -= transform.forward;
}
if (Input.GetKey(KeyCode.D))
{
targetDirection += transform.right;
}
transform.position += targetDirection * movementSpeed * Time.deltaTime;
}
private void HandleRotationMovement()
{
yaw = (yaw + Input.GetAxis("Mouse X")) % 360f;
pitch = (pitch - Input.GetAxis("Mouse Y")) % 360f;
transform.rotation = Quaternion.Euler(new Vector3(pitch, yaw, 0));
}
}
}