Jump to content

Scripting a Character Controller in Lua


_Assassin_
 Share

Go to solution Solved by AggrorJorn,

Recommended Posts

I just recently got into development with the Leadwerks engine and am interested in developing a project from the First-Person perspective. As I am new to Lua and an inexperienced programmer (programmed some office-based applications in VB.NET) I am unsure how to get started. The only tutorials I can find specifically on the scripting of a character controller is over 6 years old. In short: What are the very basic requirements for creating a simple character controller?

Link to comment
Share on other sites

  • Solution

This API command comes with a complete sample on how to set up a basic character controller.

https://www.leadwerks.com/learn?page=API-Reference_Object_Entity_SetInput

Alternatively you can look at or use the FirstPerson script. This script is located in the scripts folder in any project which uses the game template "FirstPersonShooter"

  • Upvote 1
Link to comment
Share on other sites

12 hours ago, DoomSlayer said:

Only if the FPSController script had crouch too :/

Their is references to crouching in the default FPS script but I could never get it to work myself. It would be helpful to see how crouching would be implemented in Lua so I can recreate something like it in my script.

Link to comment
Share on other sites

Strange, you would think a 3D game engine for the development of first person and third person games would have something as fundamental as support for crouching. Not that I'm complaining or anything as that is way above my technical level anyway xD. 

You could emulate some sort of crouching mechanic by moving the camera I suppose, as long as you were using the first person perspective and made the player model invisible that is. Not a very smart way to do it I know, but you could at the very least make it look like you are crouching but very little else.

Link to comment
Share on other sites

I have used crouching with the default character controller for many years  and this was officially supported. I don't know how long it has been since this option was removed. Leadwerks did recently switch to a new version of the Newton dynamics physics engine, which migh have something to do with it.

Other than that, there are some issues with the current character controller which are pain to work with. Search for topics with 'Side scroller' and you will find plenty. 

Link to comment
Share on other sites

So is making a fully-featured first person game still possible in the engines current state? I have concerns about what I want to achieve may not be possible with this engine. 

Are their any other features that have been removed after the physics engine has been updated?

Link to comment
Share on other sites

On 8/24/2017 at 9:00 AM, jen said:

Crouching works on 4.3. You'll have to wait for an update if you want to use Character Controller Crouch in later versions.

Crouching works the same in 4.4 as it has for a couple of years and several versions with some caveats and unfortunately it is not an officially supported feature (and hasn't been since the LE2 days).  The crouch height is 1.2 meters and the debug physics body does not change size at least visually when crouched, but you will be able to move a character controller under anything taller than 1.2 meters. Also note this example shows that when crouching under something, the developer needs to prevent standing back up to prevent weird physic results from occurring - ie character slingshot across map, character pushed through the map, etc...

 

window = Window:Create("crouch height",0,0,800,600,Window.Titlebar+Window.Center)
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:SetRotation(40,0,0)
camera:Move(0,0,-8)
light = DirectionalLight:Create()
light:SetRotation(35,35,0)
camera:SetDebugPhysicsMode(true)

ground = Model:Box(10,1,10)
ground:SetPosition(0,-0.5,0)
ground:SetColor(0,1,0)
shape = Shape:Box(0,0,0, 0,0,0, 10,1,10)
ground:SetShape(shape)
shape:Release()

box1 = Model:Box(1,1.2,1)
box1:SetPosition(-2,0.6,-1)
box2 = Model:Box(1,1.2,1)
box2:SetPosition(2,0.6,-1)
box3 = Model:Box(5,1,1)
box3:SetPosition(0,1.7,-1)
shape = Shape:PolyMesh((box1:GetSurface(0)))
box1:SetShape(shape)
shape = Shape:PolyMesh((box2:GetSurface(0)))
box2:SetShape(shape)
shape = Shape:PolyMesh((box3:GetSurface(0)))
box3:SetShape(shape)
shape:Release()

player = Pivot:Create()
visiblecapsule = Model:Cylinder(16,player)
visiblecapsule:SetScale(.8,1.8,.8)
visiblecapsule:SetPosition(0,.9,0)
player:SetPosition(-4,0,0)
player:SetMass(1)
player:SetPhysicsMode(Entity.CharacterPhysics)
crouch = false

while window:KeyHit(Key.Escape)==false do
	if window:Closed() then break end

	move = ((window:KeyDown(Key.W) and 1 or 0) - (window:KeyDown(Key.S) and 1 or 0))*4
	strafe = ((window:KeyDown(Key.D)and 1 or 0) - (window:KeyDown(Key.A) and 1 or 0))*4
	jump = (window:KeyHit(Key.Space) and 1 or 0)*8
	if window:KeyHit(Key.C) then crouch = not crouch end
	player:SetInput(0,move,strafe,jump,crouch)

	Time:Update()
	world:Update()
	world:Render()
	
	context:SetBlendMode(Blend.Alpha)
	context:DrawText("Press WSAD to move and C to toggle crouch",2,2)
	context:DrawText("Crouched: "..tostring(crouch),2,22)
	context:SetBlendMode(Blend.Solid)
	context:Sync(true)

end

 

crouch.jpg

  • Like 1
  • Upvote 4

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

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...