-
Notifications
You must be signed in to change notification settings - Fork 2
/
02_12 Handling Mouse Movement in Lua.lua
79 lines (55 loc) · 2.6 KB
/
02_12 Handling Mouse Movement in Lua.lua
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
-- movement
local movement = {
Properties = {
moveSpeed = {default = 3.0, description = "Movement Speed", suffix = "m/s"},
rotateSpeed = {default = 0.01, desription = "Rotation Speed", suffix = ""},
},
}
function movement:OnActivate()
-- Set key and mouse bindings
self.forwardId = InputEventNotificationId("Forward")
self.ForwardBus = InputEventNotificationBus.Connect(self, self.forwardId)
self.backwardId = InputEventNotificationId("Backward")
self.BackwardBus = InputEventNotificationBus.Connect(self, self.backwardId)
self.leftId = InputEventNotificationId("Left")
self.LeftBus = InputEventNotificationBus.Connect(self, self.leftId)
self.rightId = InputEventNotificationId("Right")
self.RightBus = InputEventNotificationBus.Connect(self, self.rightId)
self.mouseXId = InputEventNotificationId("MouseX")
self.MouseXBus = InputEventNotificationBus.Connect(self, self.mouseXId)
self.mouseYId = InputEventNotificationId("MouseY")
self.MouseYBus = InputEventNotificationBus.Connect(self, self.mouseYId)
end
function movement:OnHeld(inputValue)
local mSpeed = self.Properties.moveSpeed
local playerDir = TransformBus.Event.GetWorldTM(self.entityId)
local forwardV = playerDir:GetColumn(1) --returns direction facing
local sideV = playerDir:GetColumn(0) -- returns side (right) direction
local upV = playerDir:GetColumn(2)
local rSpeed = self.Properties.rotateSpeed
--Handle input
if (InputEventNotificationBus.GetCurrentBusId() == self.forwardId) then
TransformBus.Event.MoveEntity(self.entityId, forwardV * mSpeed * inputValue)
Debug.Log("Moved the object Forward")
elseif (InputEventNotificationBus.GetCurrentBusId() == self.backwardId) then
TransformBus.Event.MoveEntity(self.entityId, -forwardV * mSpeed * inputValue)
Debug.Log("Moved the object Backward")
elseif (InputEventNotificationBus.GetCurrentBusId() == self.rightId) then
TransformBus.Event.MoveEntity(self.entityId, sideV * mSpeed * inputValue)
Debug.Log("Moved the object Right")
elseif (InputEventNotificationBus.GetCurrentBusId() == self.leftId) then
TransformBus.Event.MoveEntity(self.entityId, -sideV * mSpeed * inputValue)
Debug.Log("Moved the object Left")
end
if (InputEventNotificationBus.GetCurrentBusId() == self.MouseXId) then
TransformBus.Event.RotateAroundLocalZ(self.entityId, -inputValue * rSpeed)
Debug.Log("Mouse Movement X")
end
if (InputEventNotificationBus.GetCurrentBusId() == self.MouseYId) then
TransformBus.Event.RotateAroundLocalX(self.entityId, -inputValue * rSpeed)
Debug.Log("Mouse Movement Y")
end
end
function movement:OnDeactivate()
end
return movement