Jump to content

Terrain Slide Code (Lua)


Haydenmango
 Share

Recommended Posts

I figured out how to do a basic terrain slide for the character controller using some code! I thought someone might find this useful since character controllers can climb really steep slopes at the moment.

 

Code:

 

local pickInfo=PickInfo()
local campos=self.camera:GetPosition()
local playerPos = self.entity:GetPosition()
if (App.world:Pick(campos,playerPos-Vec3(0,1,0),pickInfo,0,true,Collision.Scene)) then
if not pickInfo.entity.script then
if pickInfo.normal.y<.41 then
local dir = Vec2(pickInfo.position.z-campos.z,pickInfo.position.x-campos.x):Normalize()
self.entity:SetInput(-Math:ATan2(dir.y,-dir.x), 10+pickInfo.position.y, 0, 0, false, 1.0, 0.5, true)
campos = Vec3(playerPos.x, campos.y ,playerPos.z)
if campos.y<playerPos.y + self.eyeheight then
campos.y = Math:Curve(playerPos.y + self.eyeheight, campos.y, 2)
else
campos.y = playerPos.y + self.eyeheight
end
self.camera:SetPosition(campos)
return
end
end
end

 

Usage:

 

This code goes in the UpdatePhysics() function and must be placed before your characters SetInput() function to override it.

The value here - if pickInfo.normal.y<.41 then - (.41) can be changed to a value between 0 (you can climb anything) and 1 (you can't climb anything).

Also the value here - SetInput(-Math:ATan2(dir.y,-dir.x), 10+pickInfo.position.y, 0, 0, false, 1.0, 0.5, true) - (10+pickInfo.position.y) can be changed to make you slide faster or slower.

The way the code is set up it only checks for entities without a script with Scene Collision which includes terrain.

 

Example Usage:

 

if window:KeyHit(Key.Space) then
jump = 9.2
self.entity:EmitSound(self.sound.jump,5,.12,1,false)
playerMovement = playerMovement * 1.9
else
local pickInfo=PickInfo()
local campos=self.camera:GetPosition()
local playerPos = self.entity:GetPosition()
if (App.world:Pick(campos,playerPos-Vec3(0,1,0),pickInfo,0,true,Collision.Scene)) then
if not pickInfo.entity.script then
if pickInfo.normal.y<.41 then
local dir = Vec2(pickInfo.position.z-campos.z,pickInfo.position.x-campos.x):Normalize()
self.entity:SetInput(-Math:ATan2(dir.y,-dir.x), 10+pickInfo.position.y, 0, 0, false, 1.0, 0.5, true)
campos = Vec3(playerPos.x, campos.y ,playerPos.z)
if campos.y<playerPos.y + self.eyeheight then
 campos.y = Math:Curve(playerPos.y + self.eyeheight, campos.y, 2)
else
 campos.y = playerPos.y + self.eyeheight
end
self.camera:SetPosition(campos)
return
end
end
end
self:UpdateFootsteps()
end

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

 

Let me know if there is a better way to do this and Enjoy!

  • Upvote 1

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

@gamecreator - No Problem! If I knew C++ I would post a version for that as well.

 

@Josh - I just got bored and decided to give it a try because it really changes the way I build my map. Now instead of placing rocks to block paths I can just place steep terrain slopes! :D

As soon as it is officially implemented I will gladly throw this code out because I have found certain issues.

 

Issues Found:

On certain slopes the slide will push you up the slope instead of down due to the pickInfo.position not always hitting the correct spot which makes you face the slope and move forward instead of facing away from the slope and moving forward.... I am starting to think that using a check in a Collision() function might work better for getting a position than doing the pick but I haven't tried it yet.

Most of the time it can be fixed by sculpting your terrain a bit more in the areas where you have trouble.

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

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