Jump to content

Controller not rotating unless moving


Recommended Posts

To Replicate:

 

 

1) Create Lua project

2) Create 'prefabs' folder in assets tab

3) Create a cube, set it's physics to character controller, save as prefab named 'player' (the issue isn't prefab related but this will work with the code below

 

4) Open App.lua and remove everything and copy the code below and paste it in

5) Run the game and notice that unless you are moving the rotation of the character controller doesn't work

 

 

 


--This function will be called once when the program starts
function App:Start()

--Set the application title
self.title = "Z Is 4 Zombie"

--Create a window
self.window = Window:Create(self.title)
self.window:HideMouse()

--Create the graphics context
self.context = Context:Create(self.window, 0)
if self.context == nil then return false end

--Create a world
self.world = World:Create()

--Load a map
local mapfile = System:GetProperty("map", "Maps/start.map")
if Map:Load(mapfile) == false then return false end

self.camera = Camera:Create()
self.camera:SetFOV(70)

self.playerLight = SpotLight:Create()
self.playerLight:SetIntensity(3)

self.cameraRotation = Vec3()

self.player = Prefab:Load("Prefabs/player.pfb")
self.moveSpeed = 3

self:CameraFollow()

return true
end

function App:CameraFollow()
-- over the shoulder view
self.camera:SetPosition(self.player:GetPosition())
self.camera:SetRotation(self.player:GetRotation())
self.camera:Move(.75, 1.8, -1)
self.camera:Turn(0, -5, 0)

local camY = self.camera:GetRotation().y
self.camera:SetRotation(self.cameraRotation.x, camY, 0)

-- a light so the player model stands out
self.playerLight:SetPosition(self.player:GetPosition())
self.playerLight:SetRotation(self.player:GetRotation())
self.playerLight:Move(3.75, .75, 0)
self.playerLight:Turn(0, -90, 0)
end

function App:UpdateGame()
local move = ((self.window:KeyDown(Key.W) and 1 or 0) - (self.window:KeyDown(Key.S) and 1 or 0)) * Time:GetSpeed() * self.moveSpeed
local strafe = ((self.window:KeyDown(Key.D)and 1 or 0) - (self.window:KeyDown(Key.A) and 1 or 0)) * Time:GetSpeed() * (self.moveSpeed * .66)

-- get the mouse movement
local currentMousePos = self.window:GetMousePosition()
local mouseDifference = Vec3()

mouseDifference.x = currentMousePos.x - 100
mouseDifference.y = currentMousePos.y - 100

-- adjust and set the camera rotation
local tempX = self.cameraRotation.x + (mouseDifference.y / 15)
if tempX > -25 and tempX < 25 then
self.cameraRotation.x = tempX
end

self.cameraRotation.y = self.cameraRotation.y + mouseDifference.x / 15
self.window:SetMousePosition(100, 100)

self.player:SetInput(self.cameraRotation.y, move, strafe)
end

--This is our main program loop and will be called continuously until the program ends
function App:Loop()

--If window has been closed, end the program
if self.window:Closed() or self.window:KeyDown(Key.Escape) then return false end

--Update the app timing
Time:Update()



--Update the world
self.world:Update()

self:UpdateGame()
self:CameraFollow()

--Render the world
self.world:Render()

self.context:SetBlendMode(Blend.Alpha)

self.context:DrawText("Cam Rotation Y:"..self.cameraRotation.y, 0, 0)

-- draw crosshairs
self.context:SetColor(1, 0, 0, 1)
self.context:DrawLine(self.context:GetWidth() / 2, (self.context:GetHeight() / 2) - 15, self.context:GetWidth() / 2, (self.context:GetHeight() / 2) + 15)
self.context:DrawLine((self.context:GetWidth() / 2) - 15, self.context:GetHeight() / 2, (self.context:GetWidth() / 2) + 15, self.context:GetHeight() / 2)
self.context:SetColor(0, 0, 0, 1)

self.context:SetBlendMode(Blend.Solid)

--Refresh the screen
self.context:Sync()

--Returning true tells the main program to keep looping
return true
end

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...