You need to reset the values of the camera variables (camerapitch, camerayaw,and camera) and create a new controller. You are only calling them in the beginning and then you are freeing the frame work then never initializing the values again. Easiest is to just make a function out of the controller creation with these variables included and then call them where you are setting the initial controller and then inside the main loop's if-statement when you are loading a new scene.
require("Scripts/constants/collision_const")
require("Scripts/constants/engine_const")
require("Scripts/LinkedList")
require("Scripts/filesystem")
require("Scripts/math/math")
function round(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
function playercreation() --Function to create a player controller and set camera variables
controller=CreateController(1.8,0.45,0.25,45)
controller:SetCollisionType(COLLISION_CHARACTER,0)
controller:SetMass(10)
if standalone==1 then
controller:SetPosition(Vec3(-20,1,-5))
else
controller:SetPosition(fw.main.camera.position)
end
camerapitch=fw.main.camera.rotation.x
camerayaw=fw.main.camera.rotation.y-110
camera = fw.main.camera
end
BLEND_NONE=0
BLEND_ALPHA=1
if fw==nil then --we are not in Editor
RegisterAbstractPath("")
Graphics(1680,1050)
fw=CreateFramework()
scene=LoadScene("Maps/terrain_base.sbx")
scene:SetCollisionType(COLLISION_SCENE)
TFilter(1)
AFilter(4)
standalone=1
end
dx=0.0
dy=0.0
camerapitch=0.0
camerayaw=0.0
move=0.0
strafe=0.0
playercreation()--Call the controller function
HideMouse()
MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2)
--position
function DrawHUD(contr)
SetBlend(BLEND_ALPHA)
DrawText("position: "..round(contr.position.x,1)..","..round(contr.position.y,1)..","..round(contr.position.z,1),1,FontHeight()*1)
--DrawText("rot: "..round(contr.rotation.x,3)..","..round(contr.rotation.y,3)..","..round(contr.rotation.z,3),1,FontHeight()*6)
SetBlend(BLEND_NONE)
end
--main function
while KeyHit(KEY_ESCAPE)==0 do
jump=KeyHit(KEY_SPACE)*6.0
if controller:IsAirborne()==1 then jump=0 end
local time = AppTime()/3200.0
local frame = time*(179.0-96.0)+96.0
frame=Clamp( frame, 96, 179 )
--Camera look
gx=Round(GraphicsWidth()/2)
gy=Round(GraphicsHeight()/2)
dx=Curve((MouseX()-gx)/4.0,dx,3.0/AppSpeed())
dy=Curve((MouseY()-gy)/4.0,dy,3.0/AppSpeed())
MoveMouse(gx,gy)
camerapitch=camerapitch+dy
camerayaw=camerayaw-dx
camerapitch=math.min(camerapitch,90)
camerapitch=math.max(camerapitch,-89.99)
fw.main.camera:SetRotationf(camerapitch,camerayaw,0,1)
movespeed=4
movesmoothing=10
if controller:IsAirborne()==1 then
movesmoothing=200
end
--Player movement
move=Curve( (KeyDown(KEY_W)-KeyDown(KEY_S))*movespeed,move,movesmoothing)
strafe=Curve( (KeyDown(KEY_D)-KeyDown(KEY_A))*movespeed,strafe,movesmoothing)
--Update controller
controller:Update(camerayaw,move,strafe,jump,40,10)
fw:Update()
--Position camera
camera:SetPositionf(controller.position.x,controller.position.y+0.8,controller.position.z,1)
time=AppTime()
fw:Render()
DrawHUD(controller)
--new Framework---------------------------------------------------------------------------------------------------
if KeyHit(KEY_F)==1 then
FreeFramework(fw)
fw=CreateFramework()
SetGlobalObject("fw",fw)
scene=LoadScene("Maps/terrain_base2.sbx")
playercreation()-- call the controller function again to create new controller and camera variables
end
------------------------------------------------------------------------------------------------------------------
Flip(0)
end
controller:Free()
ShowMouse()