I'm just going to quickly post these 2 bits of code:
Top Down Camera:
Script.target = nil--Entity "Target"
Script.distance = 10--float
Script.debugphysics = false--bool
Script.debugnavigation = false--bool
Script.pitch = 20--float
Script.height = 10--float
function Script:Start()
if self.target==nil then return end
--debug functions for viewing physics or navigation in game
self.entity:SetDebugPhysicsMode(self.debugphysics)
self.entity:SetDebugNavigationMode(self.debugnavigation)
--Set the camera's rotation
self.entity:SetRotation(self.pitch,0,0)
end
function Script:UpdatePhysics()
--Exit the function if the target entity doesn't exist
if self.target==nil then return end
--Get the target entity's position, in global coordinates
local p0 = self.target:GetPosition(true)
--Calculate the new camera offset
local offset = Vec3(p0.x,p0.y+self.height,p0.z-self.distance)
--Add our original offset vector to the target entity position
p0 = offset
self.entity:SetPosition(p0,true)
end
Character movements: (on a goblin model)
Script.camera = nil --Entity "camera"
--Define animation sequences
Script.sequence={}
Script.sequence.walk=5
Script.sequence.idle=6
function Script:Start()
self.currentyrotation = self.entity:GetRotation().y
self.modelrotation = -90
App.window:ShowMouse()
end
function Script:UpdatePhysics()
local window = Window:GetCurrent()
local move = (window:MouseDown(Key.LButton) and 1 or 0)
local playerscreenpos = self.camera:Project(self.entity:GetPosition(true))
local mousescreenpos = window:GetMousePosition()
local vectorbetween = mousescreenpos - playerscreenpos
self.currentyrotation = Math:ATan2(vectorbetween.y, vectorbetween.x) + self.modelrotation
self.entity:SetInput(self.currentyrotation,move,0)
if move ~= 0 then
self.entity.animationmanager:SetAnimationSequence(self.sequence.walk,0.04,200)
else
self.entity.animationmanager:SetAnimationSequence(self.sequence.idle,0.05,200)
end
end
To understand the code basically
Math::ATan2 finds the angle between the mouse and the player
Camera::Project takes the character position in global space and converts it to screen space.