Control character with WASD in 8 directions

Hello, I need your help again. A very simple and not so simple question.
I want to make my character walk and turn in 8 directions.
Like if you press the W button, it turns north, if you press W+A it turns northwest. If you press the S button it turns south… You got the point.

Has anyone an idea how to do that?

I’m very grateful for replies!

Thanks!

Something along this maybe:


# ./controls.py
# execute this script in a Python controller in module mode: "controls.movement"
import mathutils
import bge

def movement(controller):
    forward = controller.sensors.get("forward")
    backward = controller.sensors.get("backward")
    left = controller.sensors.get("left")
    right = controller.sensors.get("right")

    direction = mathutils.Vector((0, 0, 0))

    if forward.positive:
        direction.y += 1
    if backward.positive:
        direction.y -= 1
    if left.positive:
        direction.x += 1
    if right.positive:
        direction.x -= 1

    direction.normalize()
    controller.owner.alignAxisToVect(direction, 1)

You can look at the doc and play with the values maybe: https://docs.blender.org/api/blender_python_api_current/bge.types.KX_GameObject.html#bge.types.KX_GameObject.alignAxisToVect

with vectors is indeed a good way, i got a directional movement system ready to go in my sig if you need it.

@Cotaks
Thank you for your reply. I downloaded your template for the 8 directional movement… But it doesn’t work. I mean when I start the engine nothing happens, I can’t control the player :frowning:

I would LOVE to use this in my game.

Plz help :smiley:

@WKnight02
This looks interesting… I tried to use it but it doesn’t really work. I’m doing something wrong. Can you maybe provide me a quick tutorial how to set up this script?

THANKS! :slight_smile:

@Cotaks
Nevermind, now it is working. I deleted the “settings” script… LOVE IT.

THANK YOU SO MUCH :DDDD

@Cotaks
I have a question. My player moves fine when I set the physics type to “dynamic”, but I need it to be a “character”.
If it is set to “character” the player won’t move. Any chance to change that the player moves when set to “character”?
Oh, and the “action” output of the player movement is really cool! Have you an idea how to add the output “running”?
That would be very cool to know!

Edit: Okay, I figured the “running” output out by myself ^^

Nevermind, now it is working. I deleted the “settings” script… LOVE IT.

you should just open the settings and set the right resolution/settings. no need to remove the settings.

I have a question. My player moves fine when I set the physics type to “dynamic”, but I need it to be a “character”.

No, character settings is not the way to go, and has other programming options then dynamic. When i created the script the character setting was not a good thing to use at all, in my eyes it’s still not good, dynamic gives the best results and you can script it however you like. For simple things character setup is great to use, for complex thing dynamic is the way to go.

Have fun with it :slight_smile:

Thank you ^^