Jump to content

Working 2.5D


SpiritMacardi
 Share

Recommended Posts

So when I started working with Leadwerks, my goal was to make a 2.5D platformer. I'd been making some great progress in that regard, mainly by using Pivots to create a movement path (pressing a direction makes the player turn and move toward one of two pivots, and once a pivot is reached the next/previous set of pivots in the sequence are swapped in).

 

Now one issue had become a big problem, and it's one which I've seen others here struggle with as well: being pushed off-path by objects in the player's path. This problem nearly made me scrap my progress and switch to a different engine... But I found out how to fix it!

 

This method doesn't exactly "lock" the movement axis, but rather applies a sort of rubberband effect when the player gets too far off-course. The code isn't as streamlined as it could be, since I only just made it, but it works and I want to share it with others who may have been dealing with the same problem.

 

If you have any questions, feel free to ask!

 

distanceRight = self.entity:GetDistance(self.rightPath)
distanceLeft = self.entity:GetDistance(self.leftPath)

local correctionP = Vec3(self.entity:GetPosition(true).x - self.leftPath:GetPosition(true).x, 0, self.entity:GetPosition(true).z - self.leftPath:GetPosition(true).z)
local correctionW = Vec3(self.rightPath:GetPosition(true).x - self.leftPath:GetPosition(true).x, 0, self.rightPath:GetPosition(true).z - self.leftPath:GetPosition(true).z)
correctionP = correctionP / correctionP:DistanceToPoint(Vec3(0, 0, 0))
correctionW = correctionW / correctionW:DistanceToPoint(Vec3(0, 0, 0))
correctionP:Normalize()
correctionW:Normalize()
local correctionD = (correctionP * distanceLeft)

if correctionD:DistanceToPoint(correctionW * distanceLeft) > 0.1 then

 local correction = Vec3((correctionW.x * distanceLeft) - correctionD.x, 0, (correctionW.z * distanceLeft) - correctionD.z)
 correction = correction * self.moveSpeed
 self.entity:AddForce(correction, true)

end

Link to comment
Share on other sites

Why not setting Velocity=0 on Z axis also to avoid the Z movement ?

Did you manage to get jumping movement also blocked in X axis ?

Do you have some complete character controller script example ? i would like to try it and see if it really works in all situations smile.png

Stop toying and make games

Link to comment
Share on other sites

Trying to set the velocity never worked for me with the character controller, hence why I used AddForce. This does work for jumping too though; I made sure that this script wouldn't interfere with the Y axis, only adjust the Z and X.

 

As for the full script and examples, I'll hopefully have something to show off soon.

Link to comment
Share on other sites

Yeah, I've done this using the character physics and SetInput function. You could probably alter this for rigid body physics, but I haven't tried myself.

 

Just be sure to have this code execute after the SetInput function. Otherwise you can end up with the character being catapulted across the screen.

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