Jump to content

FPSplayer.lua with Crouching Enabled


Haydenmango
 Share

Recommended Posts

Lots of people ask me to help them get crouching to work so I edited the FPSplayer.lua slightly so that it will work with crouching. I thought I would post a link to it here since a lot of people have trouble figuring this out.

 

https://www.dropbox.com/s/rbh1iof5qubss78/Edited%20FPSplayer.lua?dl=0

  • Upvote 3

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

Thats great Hayden. Something i just figured wasnt quite ready even if its just moving the camera down to a lower level. How might we assign a specific key for this? I think its control in the script when i was looking at it. Id rather it be "C". Ill try to figure that out on my own unless you pass along a secret to us all. BTW, GREAT contributions like this will make the community stronger!

 

lines 592-595 appears to be for setting the crouch button so-

-- Check for crouching
if window:KeyHit(Key.ControlKey) then
 self.crouched = not self.crouched
end

could be made into

-- Check for crouching
if window:KeyHit(Key.C) then
 self.crouched = not self.crouched
end

 

Then it appears as if lines 597 - 618 are the crouch function.?

Asus sabertooth 990FX, AMD FX 9590 , Windows 7 Home Premium 64 Bit, 4 HDD's Western Digital Caviar Black set in Raid 0, 16 GB Crucial Ballistix Elite, Asus Radeon R9 270 X 4GB, Corsair CM750M,

Link to comment
Share on other sites

No Problem, I am glad to help!

lines 592-595 appears to be for setting the crouch button so-

-- Check for crouching

if window:KeyHit(Key.ControlKey) then

self.crouched = not self.crouched

end

 

could be made into

-- Check for crouching

if window:KeyHit(Key.C) then

self.crouched = not self.crouched

end

 

 

 

Yep, that is where you can change the Key that activates/deactivates Crouching. Thanks for pointing that out I am sure others will find that useful.

 

Then it appears as if lines 597 - 618 are the crouch function.?

 

Lines 605-610 are the lines that actually make the camera move to your crouch height and I also changed line 599 from -

self.entity:SetInput(self.camRotation.y, playerMovement.z, playerMovement.x, jump , false, 1.0, 0.5, true)

to -

self.entity:SetInput(self.camRotation.y, playerMovement.z, playerMovement.x, jump , self.crouched, 1.0, 0.5, true)

 

to activate the Character Controllers crouching function.

  • Upvote 1

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

Got myself an error. Nothing new for me but I got

attempt to perform arithemetic on field "crouchedheight" (a nil value) line 646

 

if it needs to be broken, let me play with it. I copied and pasted the new section into my FPSPlayer.lua file as there are changes made to mine already. Here is the new part pasted.

-- Check for crouching
if window:KeyHit(Key.C) then
self.crouched = not self.crouched
end

--With smoothing
--Position camera at correct height and playerPosition
self.entity:SetInput(self.camRotation.y, playerMovement.z, playerMovement.x, jump , self.crouched, 1.0, 0.5, true)
local playerPos = self.entity:GetPosition()
local newCameraPos = self.camera:GetPosition()
--local playerTempHeight = ((self:IsCrouched() == 1) and crouchHeight or playerHeight)
newCameraPos = Vec3(playerPos.x, newCameraPos.y ,playerPos.z)

if self.crouched==true then
if newCameraPos.y<playerPos.y + self.crouchedheight then
newCameraPos.y = Math:Curve(playerPos.y + self.crouchedheight, newCameraPos.y, self.camSmoothing)
else
newCameraPos.y = playerPos.y + self.crouchedheight
end
elseif newCameraPos.y<playerPos.y + self.eyeheight then
newCameraPos.y = Math:Curve(playerPos.y + self.eyeheight, newCameraPos.y, self.camSmoothing)
else
newCameraPos.y = playerPos.y + self.eyeheight
end

self.camera:SetPosition(newCameraPos)
end

 

Here is my entire file if interested in looking at it.FPSPlayer.lua

Asus sabertooth 990FX, AMD FX 9590 , Windows 7 Home Premium 64 Bit, 4 HDD's Western Digital Caviar Black set in Raid 0, 16 GB Crucial Ballistix Elite, Asus Radeon R9 270 X 4GB, Corsair CM750M,

Link to comment
Share on other sites

Well line 646 is

local pickUpX = math.floor((context:GetWidth() - self.image.hand:GetWidth()))/2

for me so that doesn't make any sense.

Have you edited the script at all? Also if you go to your characters Script tab is the Crouch Height value equal to a number?

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

OK, I forgot to add in the top section addition of the file. NOW, it throws an error about:

fpsplayer.lua" : 153 : attempt to index field 'camera' (a nil value)

 

Shouldnt have anythign to do with this right?

Asus sabertooth 990FX, AMD FX 9590 , Windows 7 Home Premium 64 Bit, 4 HDD's Western Digital Caviar Black set in Raid 0, 16 GB Crucial Ballistix Elite, Asus Radeon R9 270 X 4GB, Corsair CM750M,

Link to comment
Share on other sites

I think its the changes from the weapon pick up script since I added a line that stops the creation of the camera since I set one in the scene.

Asus sabertooth 990FX, AMD FX 9590 , Windows 7 Home Premium 64 Bit, 4 HDD's Western Digital Caviar Black set in Raid 0, 16 GB Crucial Ballistix Elite, Asus Radeon R9 270 X 4GB, Corsair CM750M,

Link to comment
Share on other sites

Ok, just looked over your script and it looks like you just need to change -

 

Script.Camera=nil

 

to

 

Script.camera=nil

 

Capitalization can change everything!

 

edit-- Also you will need

 

Script.crouched=false

Script.crouchedheight=.9 --float "Crouch Height"

 

at the top of your script for crouching.

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

back to the error on

attempt to index field 'camera' (a nil value)

 

Attached the edited file. for me, the line in question is 154

	    self.camera:SetFOV(70)

FPSPlayer.lua

Asus sabertooth 990FX, AMD FX 9590 , Windows 7 Home Premium 64 Bit, 4 HDD's Western Digital Caviar Black set in Raid 0, 16 GB Crucial Ballistix Elite, Asus Radeon R9 270 X 4GB, Corsair CM750M,

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