Jump to content

2D Platformer player script example


Josh
 Share

Recommended Posts

Pretty simple, just attach this to any entity and control with A, D, Space.

 

Don't place a camera in the scene, this will create one.

 

Script.movespeed=5--float
Script.cameradistance=40--float
Script.camerazoom=8--float
Script.orthographiceffect=1--float "Flatness"
Script.camerarange=10--float

function Script:Start()
   self.camera = Camera:Create()
   self.entity:SetPhysicsMode(Entity.CharacterPhysics)
   self.camera:SetRotation(0,0,0)
   self.camera:SetZoom(self.camerazoom*self.orthographiceffect)
   self.camera:SetRange(self.cameradistance*self.orthographiceffect-self.camerarange,self.cameradistance*self.orthographiceffect+self.camerarange)
   self.playerangle=90
   if self.entity:GetMass()==0 then self.entity:SetMass(10) end
end

function Script:UpdatePhysics()
   local window=Window:GetCurrent()
   local move=0
   if window:KeyDown(Key.D) then move = move + self.movespeed end
   if window:KeyDown(Key.A) then move = move - self.movespeed end
   local jump = 0
   if window:KeyHit(Key.Space)  then
       jump = 10
   end
   if move>0 then self.playerangle=90 end
   if move<0 then self.playerangle=-90 end
   self.entity:SetInput(self.playerangle,math.abs(move),0,jump,false, 1.0, 0.5, true)
end

function Script:UpdateWorld()
   --Update camera
   self.camera:SetPosition(self.entity:GetPosition())
   self.camera:Move(0,1.8/2,-self.cameradistance*self.orthographiceffect)
end

  • Upvote 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Some points after testing it :

- 2.5D is not possible in LE3 as collisions will make the character move in Z axis ( or is the script for 2D sprites without 3D collisions ?)

- I changed the values for the parameters of the camera and it has no effect, the camera is still very far from character.

- Giving a script without explaining how to use camera parameters and their effect is not very usefull.

Stop toying and make games

Link to comment
Share on other sites

I applied the script to a cylinder and it used the middle of the cylinder as the foot, so half the cylinder was in the ground. When I applied a character controller it also started from the middle of the cylinder. I couldn't find in the tutorials how to change that. I think my cylinder may have been created sideways though (circle facing camera) so maybe it's a simple error on my part.

 

I also noticed that doubling the speed will get the bouncing effect down a 45 degree (or so) ramp. It makes sense that a high enough speed will eventually cause this but maybe that's a bit early.

 

But I don't use Lua anyway so I'll figure it out later. Thank you regardless.

  • Upvote 1
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...