Sidescroller help with movement

Hi,

I am working on a sidescroller in BGE and I need some help with the movement. I basically want movement like in Mario. While in mid-air you can still move, but not jump, and you don’t slide about when moving. I have tried making a python movement script to do this, but I failed: I couldn’t make the character move in mid air to prevent the jumping. I also need it to kind of do the same movement as loc actuators (no damping or whatever it’s called :o), but not to bounce in and out of objects. So does anyone know of a way to do this? It would be a great help to this game.

BTW: I think it’s impossible to do this with logic bricks, so that’s why I’m using python instead.

Many thanks,
HamstaQ

My system; DynamiX is rather complex, but I’ve learned some things that may help you discover a solution that works for you.

I prefer to use rayCasts to discern where the ground and walls are; and setLinearVelocity() to move the character. To prevent jumping in mid air; only initiate the jumping script/module if the rayCast detects the ground under the characters feet. I also like to use setLocalPosition() to glue the character to the surface (Instead of letting his feet fall through.)

Hope this helps; for a video demonstration of these features in action, check out my project (see signature) If you would like; I can reduce it to a template that would be somewhat easily applied into your game. I would also have to modify it to 2.6, or 2.5, depending on which you use (I am currently using 2.49) but that wouldn’t take me too long.

For ground movement, I’ve also found that setLinearVelocity() works well. However, it seems to ignore gravity. You’ll have to disable it while in midair by means of a rayCast or collision sensor.

As for jumping and midair movement, I suggest applyForce(). It lets you push the character in a desired direction, but still allows gravity to affect the character naturally. Hope this helps.

setLinearVelocity() works just fine with gravity; provided you use it like this:

owner.setLinearVelocity([Desired X movement, Desired Y movement, owner.getLinearVelocity(True)[2]],True)

[ATTACH]170720[/ATTACH]
Try this. The logic is spaghettied together cause i didnt take the time to clean it up or set it to different states, but I think you can backwards engineer it if you like it.

Maybe limit how far someone can go while movement in air so you dont get a constant exaggerated drift.

@Redikann Yeah, the only problem is there’s no gravity control with linV.
@vreksikon, @MCStudios Do you by any chance have an example script for this?

Thanks for the support

Never mind. I managed to get it working. I just turned up the friction amount in the material settings.
Here’s the script for movement if anyone’s interested.

############################################
# SIDESCROLLER MOTION SCRIPT
# Matt Evans (HamstaQ)
############################################
import bge
from bge import logic
from bge import events
cont = logic.getCurrentController()
own = cont.owner
x_linv = own.getLinearVelocity()[0]
z_linv = own.getLinearVelocity()[2]

if logic.keyboard.events[events.AKEY] == 2:
    x_linv = -7.5
if logic.keyboard.events[events.DKEY] == 2:
    x_linv = 7.5
if cont.sensors["g"].positive and logic.keyboard.events[events.SPACEKEY] == 1:
    z_linv = 10

own.setLinearVelocity([x_linv, 0.0, z_linv], 1)

I don’t know how to get input from the arrow keys, so I have to use A and D instead. How do I get input from the arrow keys?

From the API:

bge.events.LEFTARROWKEY
bge.events.RIGHTARROWKEY
bge.events.DOWNARROWKEY
bge.events.UPARROWKEY