-
Notifications
You must be signed in to change notification settings - Fork 0
/
XRLocomotion.cs
51 lines (41 loc) · 1.01 KB
/
XRLocomotion.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
using UnityEngine;
public class XRLocomotion : MonoBehaviour
{
[SerializeField] float speed = 1f;
new Transform camera;
#if OCULUS
static Vector2 MoveAxis => OVRInput.Get(OVRInput.RawAxis2D.LThumbstick);
static Vector2 RotateAxis => OVRInput.Get(OVRInput.RawAxis2D.RThumbstick);
#else
static Vector2 MoveAxis => Vector2.zero;
static Vector2 RotateAxis => Vector2.zero;
#endif
void Awake()
{
camera = Camera.main.transform;
}
void Update()
{
Move();
Rotate();
}
void Move()
{
var movement = new Vector3();
movement.x += MoveAxis.x;
movement.z += MoveAxis.y;
if (Input.GetKey(KeyCode.W)) movement.z += speed;
if (Input.GetKey(KeyCode.A)) movement.x -= speed;
if (Input.GetKey(KeyCode.S)) movement.z -= speed;
if (Input.GetKey(KeyCode.D)) movement.x += speed;
movement = camera.TransformVector(movement);
movement.y = 0f;
movement *= Time.deltaTime;
transform.Translate(movement, Space.Self);
}
void Rotate()
{
var rotation = RotateAxis.x;
transform.Rotate(0f, rotation, 0f);
}
}