Jump to content

TPS movement tutorial


YouGroove
 Share

Recommended Posts

Simple and basic smooth TPS code, based on Rick's game and enhanced to have smooth movement.

Enjoy happy.png

 

 


import "Scripts/AnimationManager.lua"

Script.Camera = "" --entity
Script.cameraOffsetZ= -7 --float
Script.cameraOffsetY= 3 --float
function Script:Start()
   --self.btnExit = Button:Create("Exit", 0, 0, 100, 50, self, self.btnExit_Click)

   self.playerMovement = Vec3(0,0,0)
   self.cameraYaw = 0
   self.cameraPitch = 10
   self.dx = 0
   self.dy = 0
   self.mousePos = 100



   self.moveSpeed = 10

   self.baseAnimMgr = AnimationManager:Create(self.entity)

   self.entity:SetKeyValue("type", "player")


   self.state="idle"
   self.attackName=""

end

function Script:UpdateWorld()

   self.entity:SetRotation(0,180,0,true)

   self:Movement()

   self:CameraRotation()

   self:Animations()

end


function Script:attackEnd()
    self.state="idle"
end

function Script:CameraRotation()

   -- rotate yaw and tell the player about it so the character can use it in SetInput()
   local mpos = App.window:GetMousePosition()
   self.dx = Math:Curve((mpos.x - self.mousePos) / 10.0, self.dx, 3.0 / Time:GetSpeed())
   self.cameraYaw = self.cameraYaw + self.dx

   -- rotate pitch
   self.dy = Math:Curve((mpos.y - self.mousePos) / 10.0, self.dy, 3.0 / Time:GetSpeed())
   local pitch = self.cameraPitch + self.dy
   if pitch > -25 and pitch < 55 then
       self.cameraPitch = pitch
   end

   -- reset the mouse position
   App.window:SetMousePosition(self.mousePos, self.mousePos)

end


function Script:Movement()


   -- handle player movement
   self.playerMovement.z = ((App.window:KeyDown(Key.W) and 1 or 0) - (App.window:KeyDown(Key.S)and 1 or 0)) * self.moveSpeed
   --self.playerMovement.x = ((App.window:KeyDown(Key.D) and 1 or 0) - (App.window:KeyDown(Key.A)and 1 or 0)) * Time:GetSpeed() * self.moveSpeed

  if  self.state=="attack"  then
    self.playerMovement.z=0
  end   

   if self.playerMovement.z > 0 and self.state~="attack" then
       self.state="walk"
   end

   if self.playerMovement.z == 0 and self.state~="attack" then
       self.state="idle"
   end


   -- move the camera to the player and set the yaw from 3rd person view
   self.Camera:SetPosition(self.entity:GetPosition(true), true)
   self.Camera:SetRotation(Vec3(self.cameraPitch, self.cameraYaw, 0), true)

   -- offset the camera up and back
   self.Camera:Move(0, self.cameraOffsetY, self.cameraOffsetZ)

   -- rotate the model along with the camera
   self.entity:SetRotation(Vec3(0, self.cameraYaw +180, 0))

   -- move the controller in the rotation of the player
   self.entity:SetInput(self.cameraYaw, self.playerMovement.z, 0 --[[self.playerMovement.x]], 0, false, 1)


   if App.window:MouseHit(1) and  self.state~="attack"  then
   self.state="attack"  
      self.baseAnimMgr:SetAnimationSequence("attack", 0.01, 50,1,self,self.attackEnd)   
   end

end



function Script:Animations()

   if self.state=="idle"  and self.state~="attack" then
       self.baseAnimMgr:SetAnimationSequence("idle", 0.005, 200)
   end

   if self.state=="walk" and self.state~="attack" then
       self.baseAnimMgr:SetAnimationSequence("walk", 0.01, 200)
   end

end



function Script:Draw()

   -- the order we put these in matters!
   self.baseAnimMgr:Update()

end


 

 

All movement is in UpdateWorld() because you'll get smoothed physic movement instead of jittering one.

  • Upvote 2

Stop toying and make games

Link to comment
Share on other sites

  • 2 years later...

Is this script valid because if I try to use it, it does not work.  Did the code structure change since this was written?

Mike

MSI Dominator Laptop - Core i7 - 8 cores / 3ghz / 32gb RAM / Nvidia 980 GTX with 16gb vram / SSD drives

MSI Dominator Laptop - Core i7 - 8 cores / 3ghz / 32gb RAM / Nvidia 1060 GTX with 8gb vram / SSD drives

Alienware Laptop - Core i7 - 8 cores / 3ghz / 32gb RAM / Nvidia 1070 Ti with 16gb vram / SSD drives

My Patreon page: https://www.patreon.com/michaelfelkercomposer

My music for sale: https://www.tgcstore.net/category/513?format=all&amp;perpage=30&amp;textures=undefined&amp;price=all&amp;order=default&amp;artists[]=87213

Custom synths and sounds - http://www.supersynths.com

Link to comment
Share on other sites

ty. No luck with the Darkness Awaits - loaded in all black and could not find most materials even though I followed the instructions.

I'll look at the 2nd link and see if it will work for my needs.

Mike

MSI Dominator Laptop - Core i7 - 8 cores / 3ghz / 32gb RAM / Nvidia 980 GTX with 16gb vram / SSD drives

MSI Dominator Laptop - Core i7 - 8 cores / 3ghz / 32gb RAM / Nvidia 1060 GTX with 8gb vram / SSD drives

Alienware Laptop - Core i7 - 8 cores / 3ghz / 32gb RAM / Nvidia 1070 Ti with 16gb vram / SSD drives

My Patreon page: https://www.patreon.com/michaelfelkercomposer

My music for sale: https://www.tgcstore.net/category/513?format=all&amp;perpage=30&amp;textures=undefined&amp;price=all&amp;order=default&amp;artists[]=87213

Custom synths and sounds - http://www.supersynths.com

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...